# FILE: src/apps/records/models/burial_info.py
from __future__ import annotations

import uuid
from datetime import date, datetime, time, timezone
from typing import TYPE_CHECKING, Optional
from uuid import UUID

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

from src.database.base import Base

if TYPE_CHECKING:
    from src.apps.records.models.record import Record


class BurialInfo(Base):
    __tablename__ = "burial_info"

    id: Mapped[UUID] = mapped_column(
        PG_UUID(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
        server_default=text("gen_random_uuid()"),
    )

    record_id: Mapped[UUID] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("records.id", ondelete="CASCADE"),
        nullable=False,
        unique=True,
        index=True,
    )
    tenant_id: Mapped[UUID] = mapped_column(
        PG_UUID(as_uuid=True),
        ForeignKey("accounts.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )

    interment_type: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
    interment_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
    interment_time: Mapped[Optional[time]] = mapped_column(Time, nullable=True)
    casket_type: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    depth_m: Mapped[Optional[float]] = mapped_column(Numeric(5, 2), nullable=True)
    officiant: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
    attendees: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    service_notes: Mapped[Optional[str]] = mapped_column(Text, nullable=True)

    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        nullable=False,
    )

    # Relationships
    record: Mapped["Record"] = relationship("Record", back_populates="burial_info")
