"""User and DriverProfile models."""
from sqlmodel import SQLModel, Field, Relationship
from typing import Optional
from enum import Enum
from uuid import UUID, uuid4
from datetime import datetime
from sqlalchemy import Column, DateTime, func


class UserRole(str, Enum):
    ADMIN = "ADMIN"
    PASSENGER = "PASSENGER"
    DRIVER = "DRIVER"
    PROMOTER = "PROMOTER"


class VehicleType(str, Enum):
    TAXI = "taxi"
    BUS = "bus"


class User(SQLModel, table=True):
    __tablename__ = "users"

    id: Optional[UUID] = Field(default_factory=uuid4, primary_key=True)
    email: str = Field(unique=True, index=True)
    hashed_password: str
    full_name: str = Field(index=True)
    role: UserRole = Field(default=UserRole.PASSENGER)
    is_active: bool = Field(default=True)
    is_verified: bool = Field(default=False)  # For drivers/admins verification
    profile_photo_url: Optional[str] = None
    created_at: Optional[datetime] = Field(
        sa_column=Column(DateTime, server_default=func.now())
    )
    
    # Relationships
    driver_profile: Optional["DriverProfile"] = Relationship(
        back_populates="user", sa_relationship_kwargs={"uselist": False}
    )


class DriverProfile(SQLModel, table=True):
    __tablename__ = "driver_profiles"

    id: Optional[UUID] = Field(default_factory=uuid4, primary_key=True)
    user_id: UUID = Field(foreign_key="users.id")
    cedula: str
    vehicle_type: VehicleType
    license_plate: str
    photo_url: Optional[str] = None
    vehicle_photo_url: Optional[str] = None
    cooperative_name: Optional[str] = None  # Specifically for Bus
    shift: Optional[str] = None  # For Taxi schedules (e.g. "Dia,Noche")
    payment_methods: Optional[str] = None # e.g. "Efectivo,Yappi"
    speaks_english: bool = Field(default=False)
    
    # Relationship
    user: User = Relationship(back_populates="driver_profile")
