import breezyweather.domain.weather.reference.WeatherCode;
import kotlin.time.Duration;
import org.breezyweather.unit.distance.Distance;
import org.breezyweather.unit.pollen.PollenConcentration;
import org.breezyweather.unit.pollutant.PollutantConcentration;
import org.breezyweather.unit.precipitation.Precipitation;
import org.breezyweather.unit.pressure.Pressure;
import org.breezyweather.unit.ratio.Ratio;
import org.breezyweather.unit.speed.Speed;
import org.breezyweather.unit.temperature.Temperature;

CREATE TABLE dailys(
    _id INTEGER NOT NULL PRIMARY KEY,
    location_formatted_id TEXT NOT NULL,

    date INTEGER NOT NULL,

    -- daytime
    daytime_weather_text TEXT,
    daytime_weather_summary TEXT,
    daytime_weather_code TEXT AS WeatherCode,

    daytime_temperature INTEGER AS Temperature,
    daytime_temperature_source_feels_like INTEGER AS Temperature,
    daytime_temperature_apparent INTEGER AS Temperature,
    daytime_temperature_wind_chill INTEGER AS Temperature,
    daytime_humidex INTEGER AS Temperature,

    daytime_total_precipitation INTEGER AS Precipitation,
    daytime_thunderstorm_precipitation INTEGER AS Precipitation,
    daytime_rain_precipitation INTEGER AS Precipitation,
    daytime_snow_precipitation INTEGER AS Precipitation,
    daytime_ice_precipitation INTEGER AS Precipitation,

    daytime_total_precipitation_probability INTEGER AS Ratio,
    daytime_thunderstorm_precipitation_probability INTEGER AS Ratio,
    daytime_rain_precipitation_probability INTEGER AS Ratio,
    daytime_snow_precipitation_probability INTEGER AS Ratio,
    daytime_ice_precipitation_probability INTEGER AS Ratio,

    daytime_total_precipitation_duration INTEGER AS Duration,
    daytime_thunderstorm_precipitation_duration INTEGER AS Duration,
    daytime_rain_precipitation_duration INTEGER AS Duration,
    daytime_snow_precipitation_duration INTEGER AS Duration,
    daytime_ice_precipitation_duration INTEGER AS Duration,

    daytime_wind_degree REAL,
    daytime_wind_speed INTEGER AS Speed,
    daytime_wind_gusts INTEGER AS Speed,

    -- nighttime
    nighttime_weather_text TEXT,
    nighttime_weather_summary TEXT,
    nighttime_weather_code TEXT AS WeatherCode,

    nighttime_temperature INTEGER AS Temperature,
    nighttime_temperature_source_feels_like INTEGER AS Temperature,
    nighttime_temperature_apparent INTEGER AS Temperature,
    nighttime_temperature_wind_chill INTEGER AS Temperature,
    nighttime_humidex INTEGER AS Temperature,

    nighttime_total_precipitation INTEGER AS Precipitation,
    nighttime_thunderstorm_precipitation INTEGER AS Precipitation,
    nighttime_rain_precipitation INTEGER AS Precipitation,
    nighttime_snow_precipitation INTEGER AS Precipitation,
    nighttime_ice_precipitation INTEGER AS Precipitation,

    nighttime_total_precipitation_probability INTEGER AS Ratio,
    nighttime_thunderstorm_precipitation_probability INTEGER AS Ratio,
    nighttime_rain_precipitation_probability INTEGER AS Ratio,
    nighttime_snow_precipitation_probability INTEGER AS Ratio,
    nighttime_ice_precipitation_probability INTEGER AS Ratio,

    nighttime_total_precipitation_duration INTEGER AS Duration,
    nighttime_thunderstorm_precipitation_duration INTEGER AS Duration,
    nighttime_rain_precipitation_duration INTEGER AS Duration,
    nighttime_snow_precipitation_duration INTEGER AS Duration,
    nighttime_ice_precipitation_duration INTEGER AS Duration,

    nighttime_wind_degree REAL,
    nighttime_wind_speed INTEGER AS Speed,
    nighttime_wind_gusts INTEGER AS Speed,

    degree_day_heating INTEGER AS Temperature,
    degree_day_cooling INTEGER AS Temperature,

    -- sun
    sun_rise_date INTEGER,
    sun_set_date INTEGER,

    -- twilight
    twilight_rise_date INTEGER,
    twilight_set_date INTEGER,

    -- moon
    moon_rise_date INTEGER,
    moon_set_date INTEGER,

    -- moon phase
    moon_phase_angle INTEGER,

    -- aqi.
    pm25 INTEGER AS PollutantConcentration,
    pm10 INTEGER AS PollutantConcentration,
    so2 INTEGER AS PollutantConcentration,
    no2 INTEGER AS PollutantConcentration,
    o3 INTEGER AS PollutantConcentration,
    co INTEGER AS PollutantConcentration,

    -- pollen
    alder INTEGER AS PollenConcentration,
    ash INTEGER AS PollenConcentration,
    birch INTEGER AS PollenConcentration,
    chestnut INTEGER AS PollenConcentration,
    cypress INTEGER AS PollenConcentration,
    grass INTEGER AS PollenConcentration,
    hazel INTEGER AS PollenConcentration,
    hornbeam INTEGER AS PollenConcentration,
    linden INTEGER AS PollenConcentration,
    mold INTEGER AS PollenConcentration,
    mugwort INTEGER AS PollenConcentration,
    oak INTEGER AS PollenConcentration,
    olive INTEGER AS PollenConcentration,
    plane INTEGER AS PollenConcentration,
    plantain INTEGER AS PollenConcentration,
    poplar INTEGER AS PollenConcentration,
    ragweed INTEGER AS PollenConcentration,
    sorrel INTEGER AS PollenConcentration,
    tree INTEGER AS PollenConcentration,
    urticaceae INTEGER AS PollenConcentration,
    willow INTEGER AS PollenConcentration,

    -- uv.
    uv_index REAL,

    sunshine_duration INTEGER AS Duration,

    -- relative humidity
    relative_humidity_average INTEGER AS Ratio,
    relative_humidity_min INTEGER AS Ratio,
    relative_humidity_max INTEGER AS Ratio,

    -- dewpoint
    dewpoint_average INTEGER AS Temperature,
    dewpoint_min INTEGER AS Temperature,
    dewpoint_max INTEGER AS Temperature,

    -- pressure
    pressure_average INTEGER AS Pressure,
    pressure_min INTEGER AS Pressure,
    pressure_max INTEGER AS Pressure,

    -- cloud cover
    cloud_cover_average INTEGER AS Ratio,
    cloud_cover_min INTEGER AS Ratio,
    cloud_cover_max INTEGER AS Ratio,

    -- visibility
    visibility_average INTEGER AS Distance,
    visibility_min INTEGER AS Distance,
    visibility_max INTEGER AS Distance,

    UNIQUE(location_formatted_id, date) ON CONFLICT REPLACE,
    FOREIGN KEY(location_formatted_id) REFERENCES locations (formatted_id)
    ON DELETE CASCADE
);

CREATE INDEX dailys_location_formatted_id_index ON dailys(location_formatted_id);

getDailyListByLocationId:
SELECT date, daytime_weather_text, daytime_weather_summary, daytime_weather_code, daytime_temperature, daytime_temperature_source_feels_like, daytime_temperature_apparent, daytime_temperature_wind_chill, daytime_humidex, daytime_total_precipitation, daytime_thunderstorm_precipitation, daytime_rain_precipitation, daytime_snow_precipitation, daytime_ice_precipitation, daytime_total_precipitation_probability, daytime_thunderstorm_precipitation_probability, daytime_rain_precipitation_probability, daytime_snow_precipitation_probability, daytime_ice_precipitation_probability, daytime_total_precipitation_duration, daytime_thunderstorm_precipitation_duration, daytime_rain_precipitation_duration, daytime_snow_precipitation_duration, daytime_ice_precipitation_duration, daytime_wind_degree, daytime_wind_speed, daytime_wind_gusts, nighttime_weather_text, nighttime_weather_summary, nighttime_weather_code, nighttime_temperature, nighttime_temperature_source_feels_like, nighttime_temperature_apparent, nighttime_temperature_wind_chill, nighttime_humidex, nighttime_total_precipitation, nighttime_thunderstorm_precipitation, nighttime_rain_precipitation, nighttime_snow_precipitation, nighttime_ice_precipitation, nighttime_total_precipitation_probability, nighttime_thunderstorm_precipitation_probability, nighttime_rain_precipitation_probability, nighttime_snow_precipitation_probability, nighttime_ice_precipitation_probability, nighttime_total_precipitation_duration, nighttime_thunderstorm_precipitation_duration, nighttime_rain_precipitation_duration, nighttime_snow_precipitation_duration, nighttime_ice_precipitation_duration, nighttime_wind_degree, nighttime_wind_speed, nighttime_wind_gusts, degree_day_heating, degree_day_cooling, sun_rise_date, sun_set_date, twilight_rise_date, twilight_set_date, moon_rise_date, moon_set_date, moon_phase_angle, pm25, pm10, so2, no2, o3, co, alder, ash, birch, chestnut, cypress, grass, hazel, hornbeam, linden, mold, mugwort, oak, olive, plane, plantain, poplar, ragweed, sorrel, tree, urticaceae, willow, uv_index, sunshine_duration, relative_humidity_average, relative_humidity_min, relative_humidity_max, dewpoint_average, dewpoint_min, dewpoint_max, pressure_average, pressure_min, pressure_max, cloud_cover_average, cloud_cover_min, cloud_cover_max, visibility_average, visibility_min, visibility_max
FROM dailys
WHERE location_formatted_id = :locationFormattedId
ORDER BY date;

insert:
INSERT INTO dailys(location_formatted_id, date, daytime_weather_text, daytime_weather_summary, daytime_weather_code, daytime_temperature, daytime_temperature_source_feels_like, daytime_temperature_apparent, daytime_temperature_wind_chill, daytime_humidex, daytime_total_precipitation, daytime_thunderstorm_precipitation, daytime_rain_precipitation, daytime_snow_precipitation, daytime_ice_precipitation, daytime_total_precipitation_probability, daytime_thunderstorm_precipitation_probability, daytime_rain_precipitation_probability, daytime_snow_precipitation_probability, daytime_ice_precipitation_probability, daytime_total_precipitation_duration, daytime_thunderstorm_precipitation_duration, daytime_rain_precipitation_duration, daytime_snow_precipitation_duration, daytime_ice_precipitation_duration, daytime_wind_degree, daytime_wind_speed, daytime_wind_gusts, nighttime_weather_text, nighttime_weather_summary, nighttime_weather_code, nighttime_temperature, nighttime_temperature_source_feels_like, nighttime_temperature_apparent, nighttime_temperature_wind_chill, nighttime_humidex, nighttime_total_precipitation, nighttime_thunderstorm_precipitation, nighttime_rain_precipitation, nighttime_snow_precipitation, nighttime_ice_precipitation, nighttime_total_precipitation_probability, nighttime_thunderstorm_precipitation_probability, nighttime_rain_precipitation_probability, nighttime_snow_precipitation_probability, nighttime_ice_precipitation_probability, nighttime_total_precipitation_duration, nighttime_thunderstorm_precipitation_duration, nighttime_rain_precipitation_duration, nighttime_snow_precipitation_duration, nighttime_ice_precipitation_duration, nighttime_wind_degree, nighttime_wind_speed, nighttime_wind_gusts, degree_day_heating, degree_day_cooling, sun_rise_date, sun_set_date, twilight_rise_date, twilight_set_date, moon_rise_date, moon_set_date, moon_phase_angle, pm25, pm10, so2, no2, o3, co, alder, ash, birch, chestnut, cypress, grass, hazel, hornbeam, linden, mold, mugwort, oak, olive, plane, plantain, poplar, ragweed, sorrel, tree, urticaceae, willow, uv_index, sunshine_duration, relative_humidity_average, relative_humidity_min, relative_humidity_max, dewpoint_average, dewpoint_min, dewpoint_max, pressure_average, pressure_min, pressure_max, cloud_cover_average, cloud_cover_min, cloud_cover_max, visibility_average, visibility_min, visibility_max)
VALUES (:locationFormattedId, :date, :daytimeWeatherText, :daytimeweatherSummary, :daytimeWeatherCode, :daytimeTemperature, :daytimeTemperatureSourceFeelsLike, :daytimeTemperatureApparent, :daytimeTemperatureWindChill, :daytimeHumidex, :daytimeTotalPrecipitation, :daytimeThunderstormPrecipitation, :daytimeRainPrecipitation, :daytimeSnowPrecipitation, :daytimeIcePrecipitation, :daytimeTotalPrecipitationProbability, :daytimeThunderstormPrecipitationProbability, :daytimeRainPrecipitationProbability, :daytimeSnowPrecipitationProbability, :daytimeIcePrecipitationProbability, :daytimeTotalPrecipitationDuration, :daytimeThunderstormPrecipitationDuration, :daytimeRainPrecipitationDuration, :daytimeSnowPrecipitationDuration, :daytimeIcePrecipitationDuration, :daytimeWindDegree, :daytimeWindSpeed, :daytimeWindGusts, :nighttimeWeatherText, :nighttimeweatherSummary, :nighttimeWeatherCode, :nighttimeTemperature, :nighttimeTemperatureSourceFeelsLike, :nighttimeTemperatureApparent, :nighttimeTemperatureWindChill, :nighttimeHumidex, :nighttimeTotalPrecipitation, :nighttimeThunderstormPrecipitation, :nighttimeRainPrecipitation, :nighttimeSnowPrecipitation, :nighttimeIcePrecipitation, :nighttimeTotalPrecipitationProbability, :nighttimeThunderstormPrecipitationProbability, :nighttimeRainPrecipitationProbability, :nighttimeSnowPrecipitationProbability, :nighttimeIcePrecipitationProbability, :nighttimeTotalPrecipitationDuration, :nighttimeThunderstormPrecipitationDuration, :nighttimeRainPrecipitationDuration, :nighttimeSnowPrecipitationDuration, :nighttimeIcePrecipitationDuration, :nighttimeWindDegree, :nighttimeWindSpeed, :nighttimeWindGusts, :degreeDayHeating, :degreeDayCooling, :sunRiseDate, :sunSetDate, :twilightRiseDate, :twilightSetDate, :moonRiseDate, :moonSetDate, :moonPhaseAngle, :pm25, :pm10, :so2, :no2, :o3, :co, :alder, :ash, :birch, :chestnut, :cypress, :grass, :hazel, :hornbeam, :linden, :mold, :mugwort, :oak, :olive, :plane, :plantain, :poplar, :ragweed, :sorrel, :tree, :urticaceae, :willow, :uvIndex, :sunshineDuration, :relativeHumidityAverage, :relativeHumidityMin, :relativeHumidityMax, :dewpointAverage, :dewpointMin, :dewpointMax, :pressureAverage, :pressureMin, :pressureMax, :cloudCoverAverage, :cloudCoverMin, :cloudCoverMax, :visibilityAverage, :visibilityMin, :visibilityMax);

deleteDailyListForLocationId:
DELETE FROM dailys
WHERE location_formatted_id = :locationFormattedId;
