# FILE: src/apps/scheduling/models/service_event.py
from __future__ import annotations

import datetime as dt
from typing import Optional
from uuid import UUID

import sqlalchemy as sa
from sqlalchemy import Boolean, Date, DateTime, ForeignKey, Integer, String, Text, Time
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
from sqlalchemy.orm import Mapped, mapped_column

from src.database.base import TenantModel


class ServiceEvent(TenantModel):
    __tablename__ = "services"

    # Override tenant_id with explicit FK
    tenant_id: Mapped[UUID] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("accounts.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )

    record_id: Mapped[Optional[UUID]] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("records.id", ondelete="SET NULL"),
        nullable=True,
        index=True,
    )
    plot_id: Mapped[Optional[UUID]] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("plots.id", ondelete="SET NULL"),
        nullable=True,
        index=True,
    )
    section_id: Mapped[Optional[UUID]] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("sections.id", ondelete="SET NULL"),
        nullable=True,
        index=True,
    )
    assigned_to: Mapped[Optional[UUID]] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("users.id", ondelete="SET NULL"),
        nullable=True,
        index=True,
    )
    created_by: Mapped[Optional[UUID]] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("users.id", ondelete="SET NULL"),
        nullable=True,
    )

    service_type: Mapped[str] = mapped_column(String(100), nullable=False)
    # scheduled_date / scheduled_time are the canonical names used by the new API.
    # The legacy columns service_date / service_time are kept as aliases via properties
    # so older code continues to work without a data migration.
    scheduled_date: Mapped[Optional[dt.date]] = mapped_column(
        "service_date", Date, nullable=True
    )
    scheduled_time: Mapped[Optional[dt.time]] = mapped_column(
        "service_time", Time, nullable=True
    )
    duration_minutes: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    # Legal states (INDL-34): draft / awaiting_family_confirm / confirmed.
    status: Mapped[str] = mapped_column(String(50), nullable=False, default="draft")
    notes: Mapped[Optional[str]] = mapped_column(Text, nullable=True)

    # Personnel / contact
    officiant: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    family_contact_name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    family_contact_phone: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
    expected_attendees: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    equipment_needed: Mapped[Optional[str]] = mapped_column(Text, nullable=True)

    # INDL-34 additions
    decedent_name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    family_contact_email: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    event_note: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    # Free-text location labels (independent of the plot_id / section_id FKs).
    plot_location: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    section: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)

    # Family-notification flags (replace the legacy email_family / reminder_24h flags).
    notify_family_confirmation: Mapped[bool] = mapped_column(
        Boolean, nullable=False, server_default=sa.text("false"), default=False
    )
    notify_family_reminder_24h: Mapped[bool] = mapped_column(
        Boolean, nullable=False, server_default=sa.text("false"), default=False
    )
    # System-managed ARQ job handle for the 24h reminder — NEVER exposed in any
    # API response and NEVER accepted from a request body.
    reminder_job_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)

    # File
    pdf_s3_key: Mapped[Optional[str]] = mapped_column(Text, nullable=True)

    # Soft delete (column added by migration 0012)
    deleted_at: Mapped[Optional[dt.datetime]] = mapped_column(
        DateTime(timezone=True), nullable=True, default=None
    )
