# 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 Date, 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,
    )
    assigned_to: Mapped[Optional[UUID]] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("users.id", ondelete="SET NULL"),
        nullable=True,
        index=True,
    )

    service_type: Mapped[str] = mapped_column(String(100), nullable=False)
    service_date: Mapped[Optional[dt.date]] = mapped_column(Date, nullable=True)
    service_time: Mapped[Optional[dt.time]] = mapped_column(Time, nullable=True)
    duration_minutes: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    status: Mapped[str] = mapped_column(String(50), nullable=False, default="scheduled")
    notes: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    briefing_sheet_s3_key: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
