"""Session fact model — the "What we know" backing store for a FlowPilot session.

A fact is an atomic, engineer-readable statement of what has been confirmed
during troubleshooting. Facts accumulate across the session and drive the
resolution note preview.

`source_ref` is a polymorphic pointer to a task-lane item inside
`ai_sessions.pending_task_lane` JSON — it is NOT a FK. Integrity is enforced
at the service layer per the FLOWPILOT-MIGRATION design doc Section 4.2.
Phase 2 assigns stable UUIDs to those task-lane items so `source_ref` has
something reliable to point to.
"""
import uuid
from datetime import datetime, timezone
from typing import TYPE_CHECKING

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

from app.core.database import Base

if TYPE_CHECKING:
    from app.models.ai_session import AISession
    from app.models.account import Account
    from app.models.user import User


class SessionFact(Base):
    """A single fact in the What-we-know section of a session's task lane."""
    __tablename__ = "session_facts"
    __table_args__ = (
        CheckConstraint(
            "source_type IN ('question', 'diagnostic_check', 'user_note', 'ai_synthesis')",
            name="ck_session_facts_source_type",
        ),
    )

    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,
    )
    account_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("accounts.id"),
        nullable=False,
    )
    text: Mapped[str] = mapped_column(Text, nullable=False)
    source_type: Mapped[str] = mapped_column(String(32), nullable=False)
    # Pointer to a task-lane item UUID inside ai_sessions.pending_task_lane.
    # NOT a FK. Null for `user_note` and `ai_synthesis` sources.
    source_ref: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), nullable=True
    )
    source_summary: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_by: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("users.id"),
        nullable=False,
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        onupdate=lambda: datetime.now(timezone.utc),
    )
    deleted_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True
    )

    session: Mapped["AISession"] = relationship("AISession", foreign_keys=[session_id])
    account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
    creator: Mapped["User"] = relationship("User", foreign_keys=[created_by])
