"""Session handoff model — unified park/escalate with history."""
import uuid
from datetime import datetime, timezone
from typing import Optional, Any, TYPE_CHECKING

from sqlalchemy import String, Text, Boolean, DateTime, ForeignKey, CheckConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.dialects.postgresql import UUID, JSONB

from app.core.database import Base

if TYPE_CHECKING:
    from app.models.ai_session import AISession
    from app.models.session_branch import SessionBranch
    from app.models.user import User


class SessionHandoff(Base):
    """A handoff event — either parking or escalating a session.
    Dual-writes to ai_sessions.escalation_package for backward compat.
    """
    __tablename__ = "session_handoffs"
    __table_args__ = (
        CheckConstraint("intent IN ('park', 'escalate')", name="ck_session_handoffs_intent"),
        CheckConstraint("priority IN ('normal', 'elevated')", name="ck_session_handoffs_priority"),
    )

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    session_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("ai_sessions.id", ondelete="CASCADE"), nullable=False, index=True)
    account_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("accounts.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    handed_off_by: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
    intent: Mapped[str] = mapped_column(String(20), nullable=False)
    source_branch_id: Mapped[Optional[uuid.UUID]] = mapped_column(UUID(as_uuid=True), ForeignKey("session_branches.id", ondelete="SET NULL"), nullable=True)
    snapshot: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False, default=dict, comment="Branch map, status, next step, waiting on, watch out")
    ai_assessment: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    ai_assessment_data: Mapped[Optional[dict[str, Any]]] = mapped_column(JSONB, nullable=True, comment="{likely_cause, suggested_steps, confidence}")
    artifacts: Mapped[Optional[list[dict[str, Any]]]] = mapped_column(JSONB, nullable=True, comment="[{name, type, reference}]")
    engineer_notes: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    priority: Mapped[str] = mapped_column(String(20), nullable=False, default="normal")
    claimed_by: Mapped[Optional[uuid.UUID]] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
    claimed_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
    psa_note_pushed: Mapped[bool] = mapped_column(Boolean, default=False)
    psa_note_id: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    notification_sent: Mapped[bool] = mapped_column(Boolean, default=False)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))

    # Relationships
    session: Mapped["AISession"] = relationship("AISession", foreign_keys=[session_id], back_populates="handoffs")
    handed_off_by_user: Mapped["User"] = relationship("User", foreign_keys=[handed_off_by])
    source_branch: Mapped[Optional["SessionBranch"]] = relationship("SessionBranch", foreign_keys=[source_branch_id])
    claimed_by_user: Mapped[Optional["User"]] = relationship("User", foreign_keys=[claimed_by])
