"""add magento_deletion_review table for SKU deletion workflow

Revision ID: 0021_add_magento_deletion_review
Revises: 0020_add_magento_msi_registry_tables
Create Date: 2026-03-13 00:00:00.000000
"""
from alembic import op
import sqlalchemy as sa

revision = "0021_add_magento_deletion_review"
down_revision = "0020_add_magento_msi_registry_tables"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "magento_deletion_review",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("connection_id", sa.Integer(), sa.ForeignKey("magento_connections.id"), nullable=False),
        sa.Column("sku", sa.String(128), nullable=False),
        sa.Column("product_type", sa.String(32), nullable=True),
        sa.Column("parent_sku", sa.String(128), nullable=True),
        sa.Column("ingest_run_id", sa.Integer(), sa.ForeignKey("ingest_run.id"), nullable=False),
        sa.Column("flagged_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("status", sa.String(32), nullable=False, server_default="pending"),
        sa.Column("decided_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("executed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("execution_result", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    )
    op.create_unique_constraint(
        "uq_magento_deletion_review_conn_sku_run",
        "magento_deletion_review",
        ["connection_id", "sku", "ingest_run_id"],
    )
    op.create_index("ix_magento_deletion_review_connection_id", "magento_deletion_review", ["connection_id"])
    op.create_index("ix_magento_deletion_review_status", "magento_deletion_review", ["status"])
    op.create_index("ix_magento_deletion_review_sku", "magento_deletion_review", ["sku"])


def downgrade() -> None:
    op.drop_index("ix_magento_deletion_review_sku", table_name="magento_deletion_review")
    op.drop_index("ix_magento_deletion_review_status", table_name="magento_deletion_review")
    op.drop_index("ix_magento_deletion_review_connection_id", table_name="magento_deletion_review")
    op.drop_table("magento_deletion_review")
