"""suggestion workflow: extend tdstatus enum + td_notes table

Revision ID: h5c6d7e8f9a0
Revises: g4b5c6d7e8f9
Create Date: 2026-03-18
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision = "h5c6d7e8f9a0"
down_revision = "g4b5c6d7e8f9"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Extend tdstatus enum with workflow step values.
    # IF NOT EXISTS prevents errors on re-runs; values cannot be removed in downgrade.
    for val in ("submitted", "analyse", "plan", "implement", "test", "review", "finished"):
        op.execute(f"ALTER TYPE tdstatus ADD VALUE IF NOT EXISTS '{val}'")

    # Per-step notes for technical debt items (used by dashboard-improvement workflow)
    op.create_table(
        "td_notes",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True,
                  server_default=sa.text("gen_random_uuid()")),
        sa.Column("td_id", postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("technical_debt.id", ondelete="CASCADE"),
                  nullable=False, index=True),
        sa.Column("step", sa.String(30), nullable=False),
        sa.Column("author", sa.String(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),
    )


def downgrade() -> None:
    op.drop_table("td_notes")
    # Note: PostgreSQL does not support removing enum values;
    # the added tdstatus values remain after downgrade.
