"""Two-tier feature-pack model: human-curated catalog vs. project-synthesized.

feature_packs has no project-scoping column today -- every row is a shared, global
catalog entry, human-curated via a hand-written migration. Level 1 capability
composition (src/domain/feature_packs/composer.py) needs to synthesize a new pack row
at request time without ever polluting that shared catalog. `scope` is that boundary:
'catalog' (default, existing rows unaffected) is offered to codegen as a reusable,
pre-vetted option; 'project_local' is synthesized, install-only, and never listed as a
catalog choice until an explicit future promotion step (deliberately not built here).

Also the deterministic-dedup hook for a synthesized row: `requirement_fingerprint`
lets a repeat/near-duplicate composition request reuse the same row (idempotent
re-run) instead of creating quote-request-01/quote-request-02. NULL for every
human-curated 'catalog' row.

Revision ID: 128
Revises: 127
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect

revision = "128"
down_revision = "127"
branch_labels = None
depends_on = None


def upgrade() -> None:
    conn = op.get_bind()
    insp = inspect(conn)
    cols = {c["name"] for c in insp.get_columns("feature_packs")}

    if "scope" not in cols:
        op.add_column(
            "feature_packs",
            sa.Column("scope", sa.Text(), nullable=False, server_default=sa.text("'catalog'")),
        )
    if "requirement_fingerprint" not in cols:
        op.add_column(
            "feature_packs",
            sa.Column("requirement_fingerprint", sa.Text(), nullable=True),
        )
        op.create_index(
            "idx_feature_packs_requirement_fingerprint",
            "feature_packs",
            ["requirement_fingerprint"],
        )


def downgrade() -> None:
    conn = op.get_bind()
    insp = inspect(conn)
    cols = {c["name"] for c in insp.get_columns("feature_packs")}
    indexes = {i["name"] for i in insp.get_indexes("feature_packs")}

    if "idx_feature_packs_requirement_fingerprint" in indexes:
        op.drop_index("idx_feature_packs_requirement_fingerprint", table_name="feature_packs")
    if "requirement_fingerprint" in cols:
        op.drop_column("feature_packs", "requirement_fingerprint")
    if "scope" in cols:
        op.drop_column("feature_packs", "scope")
