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


# Vehicle Request Schemas
class VehicleRequestCreate(BaseModel):
    maker_code: str
    maker_name: Optional[str] = None
    model_code: str
    model_name: Optional[str] = None
    grade_code: Optional[str] = None
    grade_name: Optional[str] = None
    year_from: Optional[int] = None
    year_to: Optional[int] = None
    mileage_min: Optional[int] = None
    mileage_max: Optional[int] = None
    fuel: Optional[str] = None
    displacement_min: Optional[int] = None
    displacement_max: Optional[int] = None


class VehicleRequestResponse(BaseModel):
    id: int
    user_id: int
    maker_code: Optional[str]
    maker_name: Optional[str]
    model_code: Optional[str]
    model_name: Optional[str]
    grade_code: Optional[str]
    grade_name: Optional[str]
    year_from: Optional[int]
    year_to: Optional[int]
    mileage_min: Optional[int]
    mileage_max: Optional[int]
    fuel: Optional[str]
    displacement_min: Optional[int]
    displacement_max: Optional[int]
    status: str
    admin_reviewed_at: Optional[datetime]
    created_at: datetime

    class Config:
        from_attributes = True


# Request Vehicle (Admin recommended) Schemas
class RequestVehicleCreate(BaseModel):
    request_id: int
    car_data: dict
    is_approved: bool = False


class RequestVehicleResponse(BaseModel):
    id: int
    request_id: int
    car_data: dict
    is_approved: bool
    approved_at: Optional[datetime]
    created_at: datetime

    class Config:
        from_attributes = True


class RequestVehicleApprove(BaseModel):
    vehicle_ids: List[int]


# Purchased Vehicle Schemas
class PurchasedVehicleCreate(BaseModel):
    car_name: str
    car_data: Optional[dict] = None
    car_image: Optional[str] = None
    vehicle_price_krw: int
    domestic_cost_krw: int
    shipping_cost_usd: int
    total_cost_krw: int
    car_type: str  # small, compact
    selected_dealer_id: Optional[int] = None  # Selected dealer for commission split


class PurchasedVehicleResponse(BaseModel):
    id: int
    user_id: int
    car_name: Optional[str]
    car_data: Optional[dict]
    car_image: Optional[str]
    vehicle_price_krw: Optional[int]
    domestic_cost_krw: Optional[int]
    shipping_cost_usd: Optional[int]
    total_cost_krw: Optional[int]
    car_type: Optional[str]
    selected_dealer_id: Optional[int] = None
    dealer_commission_krw: Optional[int] = 0
    platform_commission_krw: Optional[int] = 0
    commission_paid: bool = False
    commission_paid_at: Optional[datetime] = None
    shipping_status: int
    status_updated_at: Optional[datetime]
    current_location: Optional[str]
    estimated_arrival: Optional[datetime]
    purchased_at: datetime
    delivered_at: Optional[datetime]

    class Config:
        from_attributes = True


class PurchasedVehicleUpdateStatus(BaseModel):
    shipping_status: int  # 1-7: 구매완료, 인천항, 텐진항, 자먼우드, 울란바토르, 통관, 배송완료
    current_location: Optional[str] = None
    estimated_arrival: Optional[datetime] = None


# List response with request and approved vehicles
class VehicleRequestWithVehicles(BaseModel):
    request: VehicleRequestResponse
    approved_vehicles: List[RequestVehicleResponse]

    class Config:
        from_attributes = True
