"""add vendor onboarding table

Revision ID: m1b2c3d4e5f6
Revises: d7a4a3f06394
Create Date: 2025-12-27 22:00:00.000000

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "m1b2c3d4e5f6"
down_revision: str | None = "d7a4a3f06394"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table("vendor_onboarding",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("vendor_id", sa.Integer(), nullable=False),
        # Overall status
        sa.Column("status", sa.String(length=20), nullable=False, server_default="not_started"),
        sa.Column("current_step", sa.String(length=30), nullable=False, server_default="company_profile"),
        # Step 1: Company Profile
        sa.Column("step_company_profile_completed", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        sa.Column("step_company_profile_completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("step_company_profile_data", sa.JSON(), nullable=True),
        # Step 2: Letzshop API Configuration
        sa.Column("step_letzshop_api_completed", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        sa.Column("step_letzshop_api_completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("step_letzshop_api_connection_verified", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        # Step 3: Product Import
        sa.Column("step_product_import_completed", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        sa.Column("step_product_import_completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("step_product_import_csv_url_set", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        # Step 4: Order Sync
        sa.Column("step_order_sync_completed", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        sa.Column("step_order_sync_completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("step_order_sync_job_id", sa.Integer(), nullable=True),
        # Completion tracking
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
        # Admin override
        sa.Column("skipped_by_admin", sa.Boolean(), nullable=False, server_default=sa.text("false")),
        sa.Column("skipped_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("skipped_reason", sa.Text(), nullable=True),
        sa.Column("skipped_by_user_id", sa.Integer(), nullable=True),
        # Timestamps
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        # Constraints
        sa.ForeignKeyConstraint(["vendor_id"], ["vendors.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["skipped_by_user_id"], ["users.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_vendor_onboarding_id"), "vendor_onboarding", ["id"], unique=False)
    op.create_index(op.f("ix_vendor_onboarding_vendor_id"), "vendor_onboarding", ["vendor_id"], unique=True)
    op.create_index(op.f("ix_vendor_onboarding_status"), "vendor_onboarding", ["status"], unique=False)
    op.create_index("idx_onboarding_vendor_status", "vendor_onboarding", ["vendor_id", "status"], unique=False)


def downgrade() -> None:
    op.drop_index("idx_onboarding_vendor_status", table_name="vendor_onboarding")
    op.drop_index(op.f("ix_vendor_onboarding_status"), table_name="vendor_onboarding")
    op.drop_index(op.f("ix_vendor_onboarding_vendor_id"), table_name="vendor_onboarding")
    op.drop_index(op.f("ix_vendor_onboarding_id"), table_name="vendor_onboarding")
    op.drop_table("vendor_onboarding")
