"""Pydantic schemas for Trade-In Estimator.

POST /api/v1/trade-in/estimate  →  AggregatedEstimate
"""

from __future__ import annotations

from datetime import date, datetime
from typing import Literal
from uuid import UUID

from pydantic import BaseModel, Field, model_validator


class TradeInEstimateInput(BaseModel):
    address: str = Field(min_length=3, max_length=500)
    area_m2: float = Field(gt=10, lt=500)
    rooms: int = Field(ge=0, le=10)  # 0 = студия
    floor: int = Field(ge=1, le=100)
    total_floors: int = Field(ge=1, le=100)
    year_built: int | None = Field(default=None, ge=1800, le=2100)
    house_type: Literal["panel", "brick", "monolith", "monolith_brick", "other"] | None = None
    repair_state: Literal["needs_repair", "standard", "good", "excellent"] | None = None
    has_balcony: bool | None = None

    @model_validator(mode="after")
    def _check_floor_within_total(self) -> TradeInEstimateInput:
        if self.floor > self.total_floors:
            raise ValueError("floor must be less than or equal to total_floors")
        return self


class AnalogLot(BaseModel):
    address: str
    area_m2: float
    rooms: int
    floor: int | None
    total_floors: int | None
    price_rub: int
    price_per_m2: int
    listing_date: date | None
    days_on_market: int | None
    photo_url: str | None = None


class AggregatedEstimate(BaseModel):
    estimate_id: UUID
    median_price_rub: int
    range_low_rub: int
    range_high_rub: int
    median_price_per_m2: int
    confidence: Literal["low", "medium", "high"]
    n_analogs: int
    period_months: int  # 24
    analogs: list[AnalogLot]  # top 5-10 listings
    actual_deals: list[AnalogLot]  # реальные продажи last 12 mo
    expires_at: datetime
