import time
import json
import psycopg2
import psycopg2.extensions
from psycopg2.extras import Json
import re
import pandas as pd
import requests
import xmltodict
import datetime
from html import escape
from tabulate import tabulate

allobs = []

states = ['wv', 'oh', 'va', 'ky']
ohcounties = ['-LW-', '-GL-', '-JC-', '-MS-', '-AT-', '-PY-', '-WS-', '-MG-', '-VN-']
vacounties = ['-DC-', '-BC-']
kycounties = ['-LR-', '-CT-', '-GP-', '-BD-']
datewanted = datetime.date.today().strftime("%m/%d/%Y")

try:
    for state in states:
        url = f'https://data.cocorahs.org/export/exportreports.aspx?state={state}&Format=XML&Date={datewanted}&responsefields=all'
        response = requests.get(url)
        response.raise_for_status()  # Check for HTTP errors
        data = xmltodict.parse(response.content.decode('utf-8'))  # Explicitly decode as UTF-8
        
        try:
            daily_reports = data.get('Cocorahs', {}).get('DailyPrecipReports')
            if daily_reports is None:
                print(f"No reports found for state {state}")
                continue
            reports = daily_reports.get('DailyPrecipReport')
            if reports is None:
                print(f"No reports data found for state {state}")
                continue
            # Handle case where reports might be a single dict or a list
            if isinstance(reports, dict):
                reports = [reports]

            for report in reports:
                if state == 'wv':
                    allobs.append(report)
                else:
                    for county in eval(state + 'counties'):
                        station_number = report.get('StationNumber', '')
                        if county in station_number:
                            allobs.append(report)
        except (KeyError, TypeError) as e:
            print(f"Error processing data for state {state}: {e}")
            continue

    # Process observations
    finalobs = []
    for obs in allobs:
        tempob = [
            obs.get('DateTimeStamp', ''),
            obs.get('StationNumber', ''),
            obs.get('StationName', ''),
            obs.get('TotalPrecipAmt', ''),
            obs.get('NewSnowDepth', ''),
            obs.get('TotalSnowDepth', ''),
            obs.get('Notes', '')
        ]
        finalobs.append(tempob)

    # Write to file with UTF-8 encoding
    with open('/var/www/html/work/today.txt', 'w', encoding='utf-8') as f:
        f.write(tabulate(
            finalobs,
            headers=["Date/Time of Ob (Z)", "Station Number", "Station Name",
                    "New Precip", "New Snow", "Snow Depth", "Comments"],
            tablefmt='plain'  # Changed to 'plain' for simpler text output
        ))

    # Write HTML table to today.html
    html_content = """<!DOCTYPE html>
<html>
<head>
    <title>Cocorahs Weather Data - """ + datewanted + """</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; font-weight: bold; }
        tr:nth-child(even) { background-color: #f9f9f9; }
    </style>
</head>
<body>
    <h1>Cocorahs Weather Data - """ + datewanted + """</h1>
    <table>
        <thead>
            <tr>
                <th>Date/Time of Ob (Z)</th>
                <th>Station Number</th>
                <th>Station Name</th>
                <th>New Precip</th>
                <th>New Snow</th>
                <th>Snow Depth</th>
                <th>Comments</th>
            </tr>
        </thead>
        <tbody>
"""

    for row in finalobs:
        html_content += "            <tr>\n"
        for cell in row:
            html_content += f"                <td>{escape(str(cell))}</td>\n"
        html_content += "            </tr>\n"

    html_content += """        </tbody>
    </table>
</body>
</html>"""

    with open('/var/www/html/work/today.html', 'w', encoding='utf-8') as f:
        f.write(html_content)

except requests.RequestException as e:
    print(f"Error fetching data: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")