# ================================================================
# services/data/app/models/weather.py
# ================================================================
"""Weather data models"""

from sqlalchemy import Column, String, DateTime, Float, Integer, Text, Index, Boolean
from sqlalchemy.dialects.postgresql import UUID, JSON
import uuid
from datetime import datetime, timezone

from shared.database.base import Base

class WeatherData(Base):
    __tablename__ = "weather_data"
    
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    location_id = Column(String(100), nullable=False, index=True)
    city = Column(String(50), nullable=False)
    station_name = Column(String(200), nullable=True)
    latitude = Column(Float, nullable=True)
    longitude = Column(Float, nullable=True)
    date = Column(DateTime(timezone=True), nullable=False, index=True)
    forecast_date = Column(DateTime(timezone=True), nullable=True)
    temperature = Column(Float, nullable=True)  # Celsius
    temperature_min = Column(Float, nullable=True)
    temperature_max = Column(Float, nullable=True)
    feels_like = Column(Float, nullable=True)
    precipitation = Column(Float, nullable=True)  # mm
    precipitation_probability = Column(Float, nullable=True)
    humidity = Column(Float, nullable=True)  # percentage
    wind_speed = Column(Float, nullable=True)  # km/h
    wind_direction = Column(Float, nullable=True)
    wind_gust = Column(Float, nullable=True)
    pressure = Column(Float, nullable=True)  # hPa
    visibility = Column(Float, nullable=True)
    uv_index = Column(Float, nullable=True)
    cloud_cover = Column(Float, nullable=True)
    condition = Column(String(100), nullable=True)
    description = Column(String(200), nullable=True)
    weather_code = Column(String(20), nullable=True)
    source = Column(String(50), nullable=False, default="aemet")
    data_type = Column(String(20), nullable=False)
    is_forecast = Column(Boolean, nullable=True)
    data_quality_score = Column(Float, nullable=True)
    raw_data = Column(JSON, nullable=True)
    processed_data = Column(JSON, nullable=True)
    tenant_id = Column(UUID(as_uuid=True), nullable=True, index=True)
    created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
    updated_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
    
    __table_args__ = (
        Index('idx_weather_location_date', 'location_id', 'date'),
    )

class WeatherForecast(Base):
    __tablename__ = "weather_forecasts"
    
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    location_id = Column(String(100), nullable=False, index=True)
    forecast_date = Column(DateTime(timezone=True), nullable=False)
    generated_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(timezone.utc))
    temperature = Column(Float, nullable=True)
    precipitation = Column(Float, nullable=True)
    humidity = Column(Float, nullable=True)
    wind_speed = Column(Float, nullable=True)
    description = Column(String(200), nullable=True)
    source = Column(String(50), nullable=False, default="aemet")
    raw_data = Column(Text, nullable=True)
    created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
    updated_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
    
    __table_args__ = (
        Index('idx_forecast_location_date', 'location_id', 'forecast_date'),
    )