"""Session suggested-fix model — AI-proposed resolution path for a session.

A session can have multiple suggested fixes over its lifetime as the AI's
understanding evolves. Only one is active at a time (superseded_at IS NULL);
emitting a new [SUGGEST_FIX] marker supersedes the prior active one.
"""
import uuid
from datetime import datetime, timezone
from typing import Any, TYPE_CHECKING

from sqlalchemy import (
    Text, DateTime, ForeignKey, String, Integer, 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.account import Account
    from app.models.script_template import ScriptTemplate


class SessionSuggestedFix(Base):
    """One AI-proposed fix for a FlowPilot session."""
    __tablename__ = "session_suggested_fixes"
    __table_args__ = (
        CheckConstraint(
            "confidence_pct BETWEEN 0 AND 100",
            name="ck_session_suggested_fixes_confidence_pct",
        ),
        CheckConstraint(
            "user_decision IS NULL OR user_decision IN ("
            "'one_off', 'draft_template', 'build_template', 'dismissed')",
            name="ck_session_suggested_fixes_user_decision",
        ),
        CheckConstraint(
            "status IN ('proposed', 'applied_success', 'applied_failed', "
            "'applied_partial', 'dismissed')",
            name="ck_session_suggested_fixes_status",
        ),
    )

    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,
    )
    title: Mapped[str] = mapped_column(String(200), nullable=False)
    description: Mapped[str] = mapped_column(Text, nullable=False)
    confidence_pct: Mapped[int] = mapped_column(Integer, nullable=False)
    script_template_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("script_templates.id"),
        nullable=True,
    )
    # Populated only when there's no matching template and the AI has
    # drafted a session-specific script.
    ai_drafted_script: Mapped[str | None] = mapped_column(Text, nullable=True)
    ai_drafted_parameters: Mapped[dict[str, Any] | None] = mapped_column(
        JSONB, nullable=True
    )
    user_decision: Mapped[str | None] = mapped_column(String(32), nullable=True)
    # Outcome dimension — did the fix work? Orthogonal to user_decision.
    status: Mapped[str] = mapped_column(
        String(20), nullable=False, default="proposed"
    )
    applied_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True
    )
    verified_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True
    )
    partial_notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    failure_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
    ai_outcome_proposal: Mapped[dict[str, Any] | None] = mapped_column(
        JSONB, nullable=True
    )
    # Set when a newer suggested fix supersedes this one.
    superseded_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )

    session: Mapped["AISession"] = relationship("AISession", foreign_keys=[session_id])
    account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
    script_template: Mapped["ScriptTemplate | None"] = relationship(
        "ScriptTemplate", foreign_keys=[script_template_id]
    )
