"""Fleet & Vehicle aggregates — Phase 10.2."""
from __future__ import annotations

import uuid

from sqlalchemy import Index, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import JSON

from app.core.database import Base
from app.models.base import (
    ActorAuditMixin,
    OptimisticLockMixin,
    SoftDeleteMixin,
    TenantMixin,
    TimestampMixin,
    UUIDPrimaryKeyMixin,
)
from app.models.types import FleetStatus, GUID, VehicleAssignmentStatus, VehicleStatus


class Fleet(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
    OptimisticLockMixin,
):
    __tablename__ = "fleets"

    organization_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    hub_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    status: Mapped[FleetStatus] = mapped_column(default=FleetStatus.DRAFT, nullable=False)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_fleets_tenant_code"),
        Index("ix_fleets_tenant_status", "tenant_id", "status"),
        Index("ix_fleets_org", "tenant_id", "organization_id"),
        Index("ix_fleets_tenant_deleted", "tenant_id", "is_deleted"),
    )


class VehicleType(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
    OptimisticLockMixin,
):
    __tablename__ = "vehicle_types"

    code: Mapped[str] = mapped_column(String(50), nullable=False)
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    capacity_kg: Mapped[float | None] = mapped_column(nullable=True)
    capacity_volume: Mapped[float | None] = mapped_column(nullable=True)
    capability_tags: Mapped[list | None] = mapped_column(JSON, nullable=True)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_vehicle_types_tenant_code"),
        Index("ix_vehicle_types_tenant_deleted", "tenant_id", "is_deleted"),
    )


class Vehicle(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
    OptimisticLockMixin,
):
    __tablename__ = "vehicles"

    fleet_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    vehicle_type_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    code: Mapped[str] = mapped_column(String(50), nullable=False)
    plate_number: Mapped[str | None] = mapped_column(String(32), nullable=True)
    status: Mapped[VehicleStatus] = mapped_column(
        default=VehicleStatus.AVAILABLE, nullable=False
    )
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        UniqueConstraint("tenant_id", "code", name="uq_vehicles_tenant_code"),
        Index("ix_vehicles_fleet", "tenant_id", "fleet_id"),
        Index("ix_vehicles_tenant_status", "tenant_id", "status"),
        Index("ix_vehicles_tenant_deleted", "tenant_id", "is_deleted"),
    )


class VehicleAssignment(
    Base,
    UUIDPrimaryKeyMixin,
    TenantMixin,
    TimestampMixin,
    SoftDeleteMixin,
    ActorAuditMixin,
):
    """Shell assignment linking vehicles to drivers — no dispatch engine."""

    __tablename__ = "vehicle_assignments"

    vehicle_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    driver_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
    status: Mapped[VehicleAssignmentStatus] = mapped_column(
        default=VehicleAssignmentStatus.ACTIVE, nullable=False
    )
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)

    __table_args__ = (
        Index("ix_vehicle_assignments_vehicle", "tenant_id", "vehicle_id"),
        Index("ix_vehicle_assignments_driver", "tenant_id", "driver_id"),
        Index("ix_vehicle_assignments_tenant_deleted", "tenant_id", "is_deleted"),
    )
