"""Vessel models for vikdata API.

ALL field names use snake_case to match the backend API.

NOTE: Types like vessel_type, status, ais_class are stored as strings.
Valid values come from database lookup tables, not hardcoded enums.
"""

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

from pydantic import BaseModel, Field


class VesselEquipment(BaseModel):
    """Vessel equipment configuration."""

    nmea_0183_ports: int | None = Field(default=None, ge=0, description="Number of NMEA 0183 ports")
    nmea_2000: bool | None = Field(default=None, description="Has NMEA 2000 network")
    ais_class: str | None = Field(default=None, description="AIS transponder class (from lookup)")
    gps_model: str | None = Field(default=None, description="GPS receiver model")
    radar: bool | None = Field(default=None, description="Has radar equipment")
    sonar_model: str | None = Field(default=None, description="Sonar/fish finder model")
    vms_unit: str | None = Field(default=None, description="VMS unit identifier")


class Vessel(BaseModel):
    """Vessel response model."""

    id: UUID = Field(..., description="Vessel ID")
    tenant_id: UUID = Field(..., description="Tenant ID")
    name: str = Field(..., min_length=1, max_length=255, description="Vessel name")
    imo_number: str | None = Field(default=None, max_length=20, description="IMO number")
    mmsi: str | None = Field(default=None, max_length=20, description="MMSI number")
    call_sign: str | None = Field(default=None, max_length=20, description="Radio call sign")
    flag_state: str | None = Field(default=None, max_length=3, description="Flag state ISO code")
    vessel_type: str = Field(..., description="Vessel type (from lookup)")
    vessel_subtype: str | None = Field(default=None, max_length=50, description="Vessel subtype")
    length_meters: float | None = Field(default=None, gt=0, description="Length in meters")
    gross_tonnage: float | None = Field(default=None, gt=0, description="Gross tonnage")
    year_built: int | None = Field(default=None, ge=1800, le=2100, description="Year built")
    equipment: VesselEquipment = Field(default_factory=VesselEquipment, description="Equipment config")
    metadata: dict[str, Any] = Field(default_factory=dict, description="Additional metadata")
    status: str = Field(..., description="Vessel status (from lookup)")
    is_active: bool = Field(..., description="Whether vessel is active")
    created_at: datetime = Field(..., description="Creation timestamp")
    updated_at: datetime = Field(..., description="Last update timestamp")


class VesselListItem(BaseModel):
    """Vessel list item (subset of fields)."""

    id: UUID = Field(..., description="Vessel ID")
    tenant_id: UUID = Field(..., description="Tenant ID")
    name: str = Field(..., description="Vessel name")
    vessel_type: str = Field(..., description="Vessel type (from lookup)")
    status: str = Field(..., description="Vessel status (from lookup)")
    is_active: bool = Field(..., description="Whether vessel is active")
    created_at: datetime = Field(..., description="Creation timestamp")


class CreateVesselRequest(BaseModel):
    """Create vessel request."""

    name: str = Field(..., min_length=1, max_length=255, description="Vessel name")
    vessel_type: str = Field(..., description="Vessel type (from lookup)")
    vessel_subtype: str | None = Field(default=None, max_length=50, description="Vessel subtype")
    imo_number: str | None = Field(default=None, max_length=20, description="IMO number")
    mmsi: str | None = Field(default=None, max_length=20, description="MMSI number")
    call_sign: str | None = Field(default=None, max_length=20, description="Radio call sign")
    flag_state: str | None = Field(default=None, max_length=3, description="Flag state ISO code")
    length_meters: float | None = Field(default=None, gt=0, description="Length in meters")
    gross_tonnage: float | None = Field(default=None, gt=0, description="Gross tonnage")
    year_built: int | None = Field(default=None, ge=1800, le=2100, description="Year built")
    equipment: VesselEquipment | None = Field(default=None, description="Equipment config")
    metadata: dict[str, Any] | None = Field(default=None, description="Additional metadata")


class UpdateVesselRequest(BaseModel):
    """Update vessel request."""

    name: str | None = Field(default=None, min_length=1, max_length=255, description="Vessel name")
    vessel_type: str | None = Field(default=None, description="Vessel type (from lookup)")
    vessel_subtype: str | None = Field(default=None, max_length=50, description="Vessel subtype")
    imo_number: str | None = Field(default=None, max_length=20, description="IMO number")
    mmsi: str | None = Field(default=None, max_length=20, description="MMSI number")
    call_sign: str | None = Field(default=None, max_length=20, description="Radio call sign")
    flag_state: str | None = Field(default=None, max_length=3, description="Flag state ISO code")
    length_meters: float | None = Field(default=None, gt=0, description="Length in meters")
    gross_tonnage: float | None = Field(default=None, gt=0, description="Gross tonnage")
    year_built: int | None = Field(default=None, ge=1800, le=2100, description="Year built")
    equipment: VesselEquipment | None = Field(default=None, description="Equipment config")
    metadata: dict[str, Any] | None = Field(default=None, description="Additional metadata")
    status: str | None = Field(default=None, description="Vessel status (from lookup)")


class TransferVesselRequest(BaseModel):
    """Transfer vessel to another tenant."""

    target_tenant_id: UUID = Field(..., description="Target tenant ID")
    notes: str | None = Field(default=None, description="Transfer notes")


class VesselListParams(BaseModel):
    """Query parameters for listing vessels."""

    page: int | None = Field(default=1, ge=1, description="Page number")
    per_page: int | None = Field(default=20, ge=1, le=100, description="Items per page")
    status: str | None = Field(default=None, description="Filter by status (from lookup)")
    vessel_type: str | None = Field(default=None, description="Filter by vessel type (from lookup)")
    search: str | None = Field(default=None, description="Search by name, IMO, MMSI, or call sign")
    sort: str | None = Field(default=None, description="Sort field")
    order: Literal["asc", "desc"] | None = Field(default=None, description="Sort order")
