"""add magento sync notifications, product versions, sync items rollback cols

Revision ID: 0012_add_reporting_notifications_versions
Revises: 0011_add_magento_sync_state
Create Date: 2026-03-02 01:00:00.000000

"""

from alembic import op
import sqlalchemy as sa


revision = "0012_add_reporting_notifications_versions"
down_revision = "0011_add_magento_sync_state"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "magento_sync_notifications",
        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("job_id", sa.Integer(), sa.ForeignKey("magento_sync_jobs.id"), nullable=False),
        sa.Column("channel", sa.String(32), nullable=False, server_default="email"),
        sa.Column("to_email", sa.String(255), nullable=False),
        sa.Column("subject", sa.Text(), nullable=False),
        sa.Column("body", sa.Text(), nullable=False),
        sa.Column("status", sa.String(32), nullable=False, server_default="queued"),
        sa.Column("error", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("sent_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.create_index("ix_magento_sync_notifications_connection_id", "magento_sync_notifications", ["connection_id"])
    op.create_index("ix_magento_sync_notifications_status", "magento_sync_notifications", ["status"])

    op.create_table(
        "magento_product_versions",
        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("job_id", sa.Integer(), sa.ForeignKey("magento_sync_jobs.id"), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("source", sa.String(32), nullable=False, server_default="sync"),
        sa.Column("data_hash", sa.String(64), nullable=True),
        sa.Column("images_hash", sa.String(64), nullable=True),
        sa.Column("relations_hash", sa.String(64), nullable=True),
        sa.Column("product_payload_json", sa.dialects.postgresql.JSONB(), nullable=True),
        sa.Column("media_payload_json", sa.dialects.postgresql.JSONB(), nullable=True),
        sa.Column("relations_payload_json", sa.dialects.postgresql.JSONB(), nullable=True),
    )
    op.create_index("ix_magento_product_versions_connection_id", "magento_product_versions", ["connection_id"])
    op.create_index("ix_magento_product_versions_conn_sku", "magento_product_versions", ["connection_id", "sku"])

    op.add_column(
        "magento_sync_items",
        sa.Column("version_id", sa.Integer(), sa.ForeignKey("magento_product_versions.id"), nullable=True),
    )
    op.add_column(
        "magento_sync_items",
        sa.Column("mode", sa.String(16), nullable=True, server_default="sync"),
    )


def downgrade() -> None:
    op.drop_column("magento_sync_items", "mode")
    op.drop_column("magento_sync_items", "version_id")
    op.drop_table("magento_product_versions")
    op.drop_table("magento_sync_notifications")
