import uuid
from datetime import date, datetime

from sqlalchemy import Date, DateTime, Float, ForeignKey, String, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column

from app.db.session import Base


class CustomerProfile(Base):
    """Optional, skippable profile details for a customer account —
    never required to browse, order, or check out. Kept off of User
    itself the same way app.models.delivery.CourierProfile keeps
    delivery-specific state off of it — this is role-specific
    information, not identity. Created lazily on first update, via
    app.services.customer_profile.get_or_create_profile, mirroring
    CourierProfile's own lazy-create pattern.

    default_delivery_address/latitude/longitude mirror
    app.models.order.Order's own delivery_address/delivery_latitude/
    delivery_longitude shape on purpose, so a checkout screen can
    prefill straight from this row — but nothing requires filling
    these in here first; the same fields can just be set for the first
    time from checkout itself.
    """

    __tablename__ = "customer_profiles"

    id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
    user_id: Mapped[uuid.UUID] = mapped_column(
        Uuid, ForeignKey("users.id", ondelete="CASCADE"), unique=True, nullable=False
    )
    gender: Mapped[str | None] = mapped_column(String(30), nullable=True)
    nationality: Mapped[str | None] = mapped_column(String(100), nullable=True)
    date_of_birth: Mapped[date | None] = mapped_column(Date, nullable=True)
    default_language: Mapped[str | None] = mapped_column(String(10), nullable=True)
    address: Mapped[str | None] = mapped_column(String(255), nullable=True)
    default_delivery_address: Mapped[str | None] = mapped_column(
        String(255), nullable=True
    )
    default_delivery_latitude: Mapped[float | None] = mapped_column(
        Float, nullable=True
    )
    default_delivery_longitude: Mapped[float | None] = mapped_column(
        Float, nullable=True
    )

    created_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), nullable=False
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), onupdate=func.now(), nullable=False
    )
