import requests
import json
from datetime import datetime, timezone
import re
import psycopg2
import psycopg2.extensions
import time


metarlist = ['KCRW','KHTS','KBKW','KEKN','KPKB','KCKB','KBLF','K6L4','K48I','KHLG','KI16','KLWB','KMGW','KMRB','KW22','KW99','KUNI','KJRO','KPMH','KDWU','KZZV','KLNP','KJKL','KPBX','KSYM','KFGX','KJKL','KLHQ','KRZT','KCMH']


S = requests.Session()
conn = psycopg2.connect(host='localhost', database='nws', user='nws', password='nws')
cursor = conn.cursor()




#https://aviationweather.gov/api/data/metar?ids=KCRW%2CKHTS%2CKPKB&format=json&taf=false&hours=5

fetcharray = []


def createurl(metarslist):
    baseurl = 'https://aviationweather.gov/api/data/metar?ids='
    for i, metar in enumerate(metarlist):
     if i == len(metarlist) - 1:
        baseurl = baseurl + metar
     else:
        baseurl = baseurl + metar + "%2C"
    baseurl = baseurl + "&format=json&taf=false&hours=2"
    fetcharray.append(baseurl)


def backfillmetars(metarlist):
    for i in metarlist:
        baseurl = 'https://aviationweather.gov/api/data/metar?ids='
        baseurl = baseurl + i + "&format=json&taf=false&hours=120"
        fetcharray.append(baseurl)
        #print(baseurl)

#backfillmetars(['KCRW'])
createurl(metarlist)


for k in fetcharray:
    

    content = S.get(k)
    
    
    metardict = content.json()
    #print(metardict)
    
    
    for i in metardict:
        obid = i.get('metar_id')
        # Generate a unique obid if it's None
        if obid is None:
            # Use a combination of icaoId and reportTime to generate a unique identifier
            icao = i.get('icaoId')
            obtime = i.get('reportTime')
            if icao is not None and obtime is not None:
                obid = f"{icao}_{obtime}"
            else:
                # If both are None, we can't generate a reliable ID, so skip this record
                print(f"Skipping record due to missing icaoId or reportTime: {i}")
                continue
        else:
            icao = i.get('icaoId')
            obtime = i.get('reportTime')
            
        temp = i.get('temp')
        dewp = i.get('dewp')
        visib = i.get('visib')
        wx = i.get('wxString')
        precip1 = i.get('precip')
        precip3 = i.get('precip3hr')
        precip6 = i.get('precip6hr')
        lat = i.get('lat')
        lon = i.get('lon')
        raw = i.get('rawOb')
        stationname = i.get('name')
        
        params = (obid, icao, obtime, temp, dewp, visib, wx, precip1, precip3, precip6, lat, lon, raw, stationname)
        sql = "INSERT INTO metars (obid, icao, obtime, temp, dewp, visib, wx, precip1, precip3, precip6, lat, lon, raw, stationname) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict do nothing"
        cursor.execute(sql,params)
    conn.commit()
conn.close()
