"""add marketplace import errors table

Revision ID: 91d02647efae
Revises: 987b4ecfa503
Create Date: 2025-12-13 13:13:46.969503

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "91d02647efae"
down_revision: str | None = "987b4ecfa503"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # Create marketplace_import_errors table to store detailed import error information
    op.create_table("marketplace_import_errors",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("import_job_id", sa.Integer(), nullable=False),
        sa.Column("row_number", sa.Integer(), nullable=False),
        sa.Column("identifier", sa.String(), nullable=True),
        sa.Column("error_type", sa.String(length=50), nullable=False),
        sa.Column("error_message", sa.Text(), nullable=False),
        sa.Column("row_data", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["import_job_id"], ["marketplace_import_jobs.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id")
    )
    op.create_index("idx_import_error_job_id", "marketplace_import_errors", ["import_job_id"], unique=False)
    op.create_index("idx_import_error_type", "marketplace_import_errors", ["error_type"], unique=False)
    op.create_index(op.f("ix_marketplace_import_errors_id"), "marketplace_import_errors", ["id"], unique=False)


def downgrade() -> None:
    op.drop_index(op.f("ix_marketplace_import_errors_id"), table_name="marketplace_import_errors")
    op.drop_index("idx_import_error_type", table_name="marketplace_import_errors")
    op.drop_index("idx_import_error_job_id", table_name="marketplace_import_errors")
    op.drop_table("marketplace_import_errors")
