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

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

from sqlalchemy import Boolean, Date, DateTime, ForeignKey, Integer, String, Text, Time
from sqlalchemy.dialects.postgresql import JSONB, 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)
    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)

    # Run-sheet / briefing
    run_sheet_milestones: Mapped[Optional[dict]] = mapped_column(JSONB, nullable=True)

    # Flags
    email_family: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
    add_to_briefing: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
    reminder_24h: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
    reminder_sent: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)

    # 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
    )
