"""Product vs category owner_scope and editable seed mappings.

Revision ID: 0072_add_attribute_owner_scope
Revises: 0071_add_v2_attribute_seed_review
Create Date: 2026-08-27 16:00:00.000000
"""

from __future__ import annotations

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

revision = "0072_add_attribute_owner_scope"
down_revision = "0071_add_v2_attribute_seed_review"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "v2_attribute",
        sa.Column("owner_scope", sa.String(length=32), nullable=False, server_default="product"),
    )
    op.create_check_constraint(
        "v2_attribute_owner_scope_valid",
        "v2_attribute",
        "owner_scope IN ('product','category')",
    )
    op.add_column(
        "v2_attribute_seed_candidate",
        sa.Column("owner_scope", sa.String(length=32), nullable=False, server_default="product"),
    )
    op.create_check_constraint(
        "v2_attribute_seed_candidate_owner_scope_valid",
        "v2_attribute_seed_candidate",
        "owner_scope IN ('product','category')",
    )
    op.add_column(
        "v2_attribute_seed_mapping_candidate",
        sa.Column("is_manual", sa.Boolean(), nullable=False, server_default=sa.text("false")),
    )
    op.add_column(
        "v2_category",
        sa.Column(
            "attributes",
            postgresql.JSONB(astext_type=sa.Text()),
            nullable=False,
            server_default=sa.text("'{}'::jsonb"),
        ),
    )


def downgrade() -> None:
    op.drop_column("v2_category", "attributes")
    op.drop_column("v2_attribute_seed_mapping_candidate", "is_manual")
    op.drop_constraint("v2_attribute_seed_candidate_owner_scope_valid", "v2_attribute_seed_candidate", type_="check")
    op.drop_column("v2_attribute_seed_candidate", "owner_scope")
    op.drop_constraint("v2_attribute_owner_scope_valid", "v2_attribute", type_="check")
    op.drop_column("v2_attribute", "owner_scope")
