from pydantic import BaseModel, Field
from typing import Dict, List, Optional, Any
from datetime import datetime


# ── Catalog ──────────────────────────────────────────────────────────────────

class DatasetVariable(BaseModel):
    short_name: str
    standard_name: Optional[str] = None
    units: Optional[str] = None
    description: Optional[str] = None  # human-readable label derived from standard_name


class CatalogDataset(BaseModel):
    dataset_id: str
    title: Optional[str] = None
    description: Optional[str] = None
    variables: List[DatasetVariable] = []
    min_longitude: Optional[float] = None
    max_longitude: Optional[float] = None
    min_latitude: Optional[float] = None
    max_latitude: Optional[float] = None
    start_datetime: Optional[str] = None
    end_datetime: Optional[str] = None


# ── Jobs ─────────────────────────────────────────────────────────────────────

class DownloadJobRequest(BaseModel):
    dataset_id: str
    variables: List[str] = Field(..., min_length=1)
    min_longitude: float
    max_longitude: float
    min_latitude: float
    max_latitude: float
    start_date: str  # YYYY-MM-DD
    end_date: str    # YYYY-MM-DD
    format: str = Field("json", pattern="^(json|csv)$")
    nome: str = Field(..., min_length=1)
    tags: List[str] = Field(default_factory=lambda: ["marine"])
    notes: str = ""
    variable_renames: Dict[str, str] = Field(default_factory=dict)  # {original: custom}


class JobStatus(BaseModel):
    job_id: str
    status: str  # pending | downloading | converting | saving | done | error
    progress: int = 0  # 0-100
    message: str = ""
    dataset_id: Optional[str] = None  # filled on done


# ── Saved Datasets ────────────────────────────────────────────────────────────

class DatasetMeta(BaseModel):
    id: str
    nome: str
    tags: List[str] = []
    created_date: str
    created_by: str
    used_last_date: Optional[str] = None
    type: str  # json | csv
    size: int
    notes: str = ""
    version: int = 1
    filename: str
    copernicus_dataset_id: str
    variables: List[str] = []
    bbox: List[float] = []  # [min_lon, min_lat, max_lon, max_lat]
    start_date: str
    end_date: str


class DatasetListResponse(BaseModel):
    datasets: List[DatasetMeta]
    count: int
