"""Draft template model — scripts generated during a session, pending templatization.

Created when an engineer picks "Run now, templatize after resolve" in the
three-option dialog. Post-resolve, the TemplatizePrompt component reads pending
drafts and lets the engineer accept (promotes to `script_templates`) or reject.
"""
import uuid
from datetime import datetime, timezone
from typing import Any, TYPE_CHECKING

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


class DraftTemplate(Base):
    """A session-generated script pending conversion to a reusable template."""
    __tablename__ = "draft_templates"
    __table_args__ = (
        CheckConstraint(
            "status IN ('pending', 'accepted', 'rejected')",
            name="ck_draft_templates_status",
        ),
    )

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
    )
    account_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("accounts.id"),
        nullable=False,
    )
    source_session_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("ai_sessions.id"),
        nullable=False,
    )
    source_user_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("users.id"),
        nullable=False,
    )
    script_body: Mapped[str] = mapped_column(Text, nullable=False)
    proposed_parameters: Mapped[dict[str, Any]] = mapped_column(
        JSONB, nullable=False
    )
    proposed_name: Mapped[str | None] = mapped_column(String(200), nullable=True)
    proposed_category_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("script_categories.id"),
        nullable=True,
    )
    status: Mapped[str] = mapped_column(
        String(32), nullable=False, default="pending"
    )
    resolved_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True
    )
    # Set when status transitions to 'accepted' and the draft is promoted
    # to a real script_templates row.
    promoted_template_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("script_templates.id"),
        nullable=True,
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
    )

    account: Mapped["Account"] = relationship("Account", foreign_keys=[account_id])
    source_session: Mapped["AISession"] = relationship(
        "AISession", foreign_keys=[source_session_id]
    )
    source_user: Mapped["User"] = relationship("User", foreign_keys=[source_user_id])
    proposed_category: Mapped["ScriptCategory | None"] = relationship(
        "ScriptCategory", foreign_keys=[proposed_category_id]
    )
    promoted_template: Mapped["ScriptTemplate | None"] = relationship(
        "ScriptTemplate", foreign_keys=[promoted_template_id]
    )
