"""add suggestions demand-weighted backlog

Revision ID: f0a1b2c3d4e5
Revises: e9f0a1b2c3d4
Create Date: 2026-07-06

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import UUID

revision = "f0a1b2c3d4e5"
down_revision = "f1a2b3c4d5e6"
branch_labels = None
depends_on = None

suggestionstage = postgresql.ENUM(
    "suggestion",
    "requirement",
    "promoted",
    "declined",
    name="suggestionstage",
    create_type=False,
)


def upgrade() -> None:
    suggestionstage.create(op.get_bind(), checkfirst=True)
    op.create_table(
        "suggestions",
        sa.Column("id", UUID(as_uuid=True), primary_key=True),
        sa.Column("domain_id", UUID(as_uuid=True), sa.ForeignKey("domains.id", ondelete="RESTRICT"), nullable=False),
        sa.Column("topic_id", UUID(as_uuid=True), sa.ForeignKey("topics.id", ondelete="SET NULL"), nullable=True),
        sa.Column("workplan_id", UUID(as_uuid=True), sa.ForeignKey("workplans.id", ondelete="SET NULL"), nullable=True),
        sa.Column("title", sa.String(length=500), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("origin", sa.String(length=200), nullable=True),
        sa.Column("origin_ref", sa.String(length=200), nullable=True),
        sa.Column("stage", suggestionstage, nullable=False, server_default="suggestion"),
        sa.Column("relevance", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("relevance_events", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("last_requested_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("base_value", sa.Float(), nullable=False, server_default="3"),
        sa.Column("job_size", sa.Float(), nullable=False, server_default="3"),
        sa.Column("relevance_weight", sa.Float(), nullable=False, server_default="1"),
        sa.Column("promoted_task_id", UUID(as_uuid=True), sa.ForeignKey("tasks.id", ondelete="SET NULL"), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
    )
    op.create_index("ix_suggestions_domain_id", "suggestions", ["domain_id"])
    op.create_index("ix_suggestions_stage", "suggestions", ["stage"])
    op.create_index("ix_suggestions_origin_ref", "suggestions", ["origin_ref"])

    op.create_table(
        "suggestion_notes",
        sa.Column("id", UUID(as_uuid=True), primary_key=True),
        sa.Column("suggestion_id", UUID(as_uuid=True), sa.ForeignKey("suggestions.id", ondelete="CASCADE"), nullable=False),
        sa.Column("stage", sa.String(length=30), nullable=False),
        sa.Column("author", sa.String(length=100), nullable=True),
        sa.Column("content", sa.Text(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
    )
    op.create_index("ix_suggestion_notes_suggestion_id", "suggestion_notes", ["suggestion_id"])

    op.create_table(
        "suggestion_relevance_bumps",
        sa.Column("id", UUID(as_uuid=True), primary_key=True),
        sa.Column("suggestion_id", UUID(as_uuid=True), sa.ForeignKey("suggestions.id", ondelete="CASCADE"), nullable=False),
        sa.Column("source", sa.String(length=50), nullable=False),
        sa.Column("source_key", sa.String(length=200), nullable=False),
        sa.Column("reason", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
    )
    op.create_index("ix_suggestion_relevance_bumps_suggestion_id", "suggestion_relevance_bumps", ["suggestion_id"])


def downgrade() -> None:
    op.drop_table("suggestion_relevance_bumps")
    op.drop_table("suggestion_notes")
    op.drop_table("suggestions")
    op.execute("DROP TYPE IF EXISTS suggestionstage")