"""add magento catalog state, media bootstrap, feed events, sync queue

Revision ID: 0013_add_pull_queue_feed_events
Revises: 0012_add_reporting_notifications_versions
Create Date: 2026-03-02 02:00:00.000000

"""

from alembic import op
import sqlalchemy as sa


revision = "0013_add_pull_queue_feed_events"
down_revision = "0012_add_reporting_notifications_versions"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "magento_catalog_state",
        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("magento_product_id", sa.Integer(), nullable=True),
        sa.Column("updated_at_magento", sa.DateTime(timezone=True), nullable=True),
        sa.Column("price", sa.Numeric(12, 4), nullable=True),
        sa.Column("special_price", sa.Numeric(12, 4), nullable=True),
        sa.Column("media_files", sa.dialects.postgresql.JSONB(), nullable=True),
        sa.Column("media_hash", sa.String(64), nullable=True),
        sa.Column("payload_hash", sa.String(64), nullable=True),
        sa.Column("last_pulled_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.create_unique_constraint(
        "uq_magento_catalog_state_conn_sku",
        "magento_catalog_state",
        ["connection_id", "sku"],
    )
    op.create_index("ix_magento_catalog_state_connection_id", "magento_catalog_state", ["connection_id"])

    op.add_column(
        "magento_media_map",
        sa.Column("magento_file", sa.Text(), nullable=True),
    )
    op.alter_column(
        "magento_media_map",
        "content_sha256",
        existing_type=sa.String(64),
        nullable=True,
    )

    op.create_table(
        "plytix_feed_events",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("label", sa.String(128), nullable=False),
        sa.Column("type", sa.String(32), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("payload", sa.dialects.postgresql.JSONB(), nullable=True),
    )
    op.create_index("ix_plytix_feed_events_label", "plytix_feed_events", ["label"])
    op.create_index("ix_plytix_feed_events_type", "plytix_feed_events", ["type"])

    op.create_table(
        "magento_sync_queue",
        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("label", sa.String(128), nullable=False),
        sa.Column("status", sa.String(32), nullable=False, server_default="queued"),
        sa.Column("queued_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("last_error", sa.Text(), nullable=True),
        sa.Column("options", sa.dialects.postgresql.JSONB(), nullable=True),
    )
    op.create_index("ix_magento_sync_queue_status", "magento_sync_queue", ["status"])
    op.create_index("ix_magento_sync_queue_connection_id", "magento_sync_queue", ["connection_id"])


def downgrade() -> None:
    op.drop_table("magento_sync_queue")
    op.drop_table("plytix_feed_events")
    op.alter_column("magento_media_map", "content_sha256", nullable=False)
    op.drop_column("magento_media_map", "magento_file")
    op.drop_table("magento_catalog_state")
