from datetime import datetime, date
from decimal import Decimal
from typing import Any
from pydantic import BaseModel, Field


class ValuationCreate(BaseModel):
    plate: str = Field(..., min_length=5, max_length=10)
    motornet_code: str | None = None
    mileage: int | None = Field(None, ge=0)
    retake_regime: str | None = None
    expected_return_date: date | None = None
    interest_fuel: str | None = None
    priority: str = "valutazione"
    notes: str | None = None


class CommentCreate(BaseModel):
    content: str = Field(..., min_length=1)
    visible_to_all: bool = True


class CommentResponse(BaseModel):
    id: int
    valuation_id: int
    user_id: int
    user_full_name: str | None = None
    content: str
    visible_to_all: bool
    created_at: datetime

    model_config = {"from_attributes": True}


class PhotoResponse(BaseModel):
    id: int
    valuation_id: int
    filename: str
    storage_path: str
    uploaded_by: int
    uploaded_at: datetime
    url: str | None = None

    model_config = {"from_attributes": True}


class HistoryResponse(BaseModel):
    id: int
    field_name: str
    old_value: str | None
    new_value: str | None
    changed_by: int
    changed_by_name: str | None = None
    changed_at: datetime

    model_config = {"from_attributes": True}


class VehicleInfo(BaseModel):
    plate: str
    vin: str | None
    vehicle_type: str | None
    registration_date: date | None
    version_label: str | None
    brand_name: str | None
    model_description: str | None
    gamma_description: str | None
    list_price: Decimal | None


class MotornetValuationResponse(BaseModel):
    id: int
    valuation_id: int
    plate: str
    motornet_code: str | None
    fetched_at: datetime
    registration_year: int | None
    registration_month: int | None
    mileage: int | None
    motornet_id: int | None
    ediz_dati: str | None
    brand_name: str | None
    model_description: str | None
    allestimento: str | None
    alimentazione: str | None
    immagine_url: str | None
    xml_url: str | None
    percorrenza_media_km: int | None
    quotazione_blu: Decimal | None
    quotazione_blu_km: Decimal | None
    quotazione_giallo: Decimal | None
    quotazione_giallo_km: Decimal | None
    quotazione_blu_totale: Decimal | None
    quotazione_giallo_totale: Decimal | None
    variazione_km: Decimal | None
    prezzo_listino: Decimal | None
    prezzo_accessori: Decimal | None
    totale_riparazioni_carrozzeria: Decimal | None
    totale_riparazioni_meccanica: Decimal | None
    raw_response: Any | None = None

    model_config = {"from_attributes": True}


class ValuationResponse(BaseModel):
    id: int
    plate: str
    user_id: int
    group_id: int | None
    motornet_code: str | None
    mileage: int | None
    retake_regime: str | None
    expected_return_date: date | None
    interest_fuel: str | None
    priority: str
    notes: str | None
    status: str
    final_value: Decimal | None
    evaluator_notes: str | None
    is_frozen: bool
    priority_rank: int
    created_at: datetime
    updated_at: datetime
    vehicle: VehicleInfo | None = None
    photos: list[PhotoResponse] = []
    comments: list[CommentResponse] = []
    history: list[HistoryResponse] = []
    motornet_valuations: list[MotornetValuationResponse] = []

    model_config = {"from_attributes": True}


class ValuationListItem(BaseModel):
    id: int
    plate: str
    status: str
    priority: str
    priority_rank: int
    mileage: int | None
    is_frozen: bool
    created_at: datetime
    updated_at: datetime
    brand_name: str | None = None
    model_description: str | None = None
    version_label: str | None = None
    final_value: Decimal | None = None

    model_config = {"from_attributes": True}


class PaginatedValuations(BaseModel):
    items: list[ValuationListItem]
    total: int
    page: int
    page_size: int
    pages: int
