"""Lookup models for database-driven enumerations.

Lookups replace hardcoded enums with database-driven values that can be:
- System-wide (available to all tenants)
- Tenant-specific (custom values for a single tenant)

Categories include:
- vessel_type: fishing, cargo, tanker, passenger, research, tug, other
- vessel_status: active, inactive, maintenance, decommissioned
- ais_class: A, B
- subscription_status: active, trialing, past_due, canceled, paused
- invoice_status: draft, pending, paid, failed, void
"""

from datetime import datetime
from typing import Any
from uuid import UUID

from pydantic import BaseModel, Field


class Lookup(BaseModel):
    """Lookup value from the database."""

    id: UUID = Field(..., description="Lookup ID")
    category: str = Field(..., description="Category (e.g., vessel_type)")
    slug: str = Field(..., description="Unique slug within category")
    name: str = Field(..., description="Human-readable name")
    description: str | None = Field(default=None, description="Description")
    sort_order: int = Field(default=0, description="Display order")
    is_active: bool = Field(default=True, description="Whether lookup is active")
    is_system: bool = Field(default=False, description="Whether lookup is system-defined")
    tenant_id: UUID | None = Field(default=None, description="Tenant ID for tenant-specific lookups")
    metadata: dict[str, Any] = Field(default_factory=dict, description="Additional metadata")
    created_at: datetime = Field(..., description="Creation timestamp")
    updated_at: datetime = Field(..., description="Last update timestamp")


class LookupCreate(BaseModel):
    """Create a tenant-specific lookup value."""

    slug: str = Field(..., min_length=1, max_length=50, pattern=r"^[a-z][a-z0-9_]*$")
    name: str = Field(..., min_length=1, max_length=100)
    description: str | None = Field(default=None, max_length=1000)
    sort_order: int = Field(default=0, ge=0)
    metadata: dict[str, Any] = Field(default_factory=dict)


class LookupUpdate(BaseModel):
    """Update a tenant-specific lookup value."""

    name: str | None = Field(default=None, min_length=1, max_length=100)
    description: str | None = Field(default=None, max_length=1000)
    sort_order: int | None = Field(default=None, ge=0)
    is_active: bool | None = Field(default=None)
    metadata: dict[str, Any] | None = Field(default=None)


class LookupCategory(BaseModel):
    """Lookups for a specific category."""

    category: str = Field(..., description="Category name")
    values: list[Lookup] = Field(default_factory=list, description="Lookup values")


class LookupsResponse(BaseModel):
    """All lookups grouped by category."""

    categories: dict[str, list[Lookup]] = Field(
        default_factory=dict, description="Lookups grouped by category"
    )


# Known categories for reference
KNOWN_CATEGORIES = [
    "vessel_type",
    "vessel_status",
    "ais_class",
    "subscription_status",
    "invoice_status",
]
