import streamlit as st
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression # type: ignore
import requests
import datetime
import json
import re

# Ładowanie danych
@st.cache_data
def load_data():
    df = pd.read_csv('rats_weather.csv')
    return df

# Przygotowanie i trenowanie modelu
def train_model(df):
    df["day"] = pd.to_datetime(df["day"])
    df["weekday"] = df["day"].dt.dayofweek < 5
    df["weekday"] = df["weekday"].astype(int)
    X = df[['low_temp', 'wind_speed', 'weekday']]
    y = df['rat_sightings']
    model = LinearRegression()
    model.fit(X, y)
    return model

# Pobranie danych prognozy pogody
def get_forecast():
    url = "https://api.weather.gov/gridpoints/OKX/34,36/forecast"
    response = requests.get(url)
    forecast_data = response.json()

    forecast_df = pd.DataFrame(forecast_data["properties"]["periods"])
    forecast_df[
        ["endTime", "isDaytime", "temperature", 
    "relativeHumidity", "windSpeed"]
    ].head()

    def get_wind_speed(row):
        numbers = [int(num) for num in re.findall(r'\d+', row)]
        return max(numbers)

    forecast_df["date"] = pd.to_datetime(forecast_df['endTime']).dt.date

    forecast_df["wind_speed"] = forecast_df["windSpeed"].apply(
        get_wind_speed
    )

    forecast_df["humidity"] = forecast_df["relativeHumidity"].apply(
        lambda x: x["value"]
    )

    daily_forecast = (
        forecast_df.groupby("date")
        .agg({
            "temperature": ["min", "max"],
            "humidity": "max",
            "wind_speed": "max"})
        .reset_index()
    )

    daily_forecast.columns = [
    "date", "low_temp", "high_temp", "humidity", "wind_speed"
    ]
    
    daily_forecast["date"] = pd.to_datetime(daily_forecast["date"])
    daily_forecast["dow"] = daily_forecast["date"].dt.dayofweek
    daily_forecast["weekday"] = (daily_forecast["dow"]<5).astype(int)

    return daily_forecast

# Aplikacja Streamlit
def main():
    st.title("Model prognozy pogody")

    data = load_data()
    model = train_model(data)

    st.write("### Prognozowana pogoda")

    forecast_data = get_forecast()
    df_forecast = pd.DataFrame(forecast_data, columns=['date', 'low_temp', 'wind_speed', 'weekday'])
    predictions = model.predict(df_forecast[['low_temp', 'wind_speed', 'weekday']])

    # Przygotowanie danych do utworzenia wykresu
    df_forecast['predictions'] = predictions
    df_forecast.set_index('date', inplace=True)
    
    # Utworzenie wykresu prognozy za pomocą wbudowanego wykresu słupkowego aplikacji Streamlit
    st.bar_chart(df_forecast['predictions'], height=200)

    st.write("### Prognoza pogody")
    st.table(df_forecast[["low_temp", "wind_speed", "predictions"]])
if __name__ == "__main__":
    main()
