"""Add frontend_type to admin_menu_configs

Revision ID: zb1l2m3n4o5p6
Revises: za0k1l2m3n4o5
Create Date: 2026-01-25

Adds frontend_type column to support both admin and vendor menu configuration:
- 'admin': Admin panel menus (super admins, platform admins)
- 'vendor': Vendor dashboard menus (configured per platform)

Also updates unique constraints to include frontend_type and adds
a check constraint ensuring user_id scope is only used for admin frontend.
"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "zb1l2m3n4o5p6"
down_revision = "za0k1l2m3n4o5"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # 1. Create the enum type for frontend_type
    frontend_type_enum = sa.Enum("admin", "vendor", name="frontendtype")
    frontend_type_enum.create(op.get_bind(), checkfirst=True)

    # 2. Add frontend_type column with default value
    op.add_column(
        "admin_menu_configs",
        sa.Column(
            "frontend_type",
            sa.Enum("admin", "vendor", name="frontendtype"),
            nullable=False,
            server_default="admin",
            comment="Which frontend this config applies to (admin or vendor)",
        ),
    )

    # 3. Create index on frontend_type
    op.create_index(
        "idx_admin_menu_configs_frontend_type",
        "admin_menu_configs",
        ["frontend_type"],
    )

    # 4. Drop old unique constraints
    op.drop_constraint("uq_platform_menu_config", "admin_menu_configs", type_="unique")
    op.drop_constraint("uq_user_menu_config", "admin_menu_configs", type_="unique")

    # 5. Create new unique constraints that include frontend_type
    op.create_unique_constraint(
        "uq_frontend_platform_menu_config",
        "admin_menu_configs",
        ["frontend_type", "platform_id", "menu_item_id"],
    )
    op.create_unique_constraint(
        "uq_frontend_user_menu_config",
        "admin_menu_configs",
        ["frontend_type", "user_id", "menu_item_id"],
    )

    # 6. Add check constraint: user_id scope only allowed for admin frontend
    op.create_check_constraint(
        "ck_user_scope_admin_only",
        "admin_menu_configs",
        "(user_id IS NULL) OR (frontend_type = 'admin')",
    )

    # 7. Create composite indexes for common queries
    op.create_index(
        "idx_admin_menu_config_frontend_platform",
        "admin_menu_configs",
        ["frontend_type", "platform_id"],
    )
    op.create_index(
        "idx_admin_menu_config_frontend_user",
        "admin_menu_configs",
        ["frontend_type", "user_id"],
    )


def downgrade() -> None:
    # Drop new indexes
    op.drop_index("idx_admin_menu_config_frontend_user", table_name="admin_menu_configs")
    op.drop_index("idx_admin_menu_config_frontend_platform", table_name="admin_menu_configs")

    # Drop check constraint
    op.drop_constraint("ck_user_scope_admin_only", "admin_menu_configs", type_="check")

    # Drop new unique constraints
    op.drop_constraint("uq_frontend_user_menu_config", "admin_menu_configs", type_="unique")
    op.drop_constraint("uq_frontend_platform_menu_config", "admin_menu_configs", type_="unique")

    # Restore old unique constraints
    op.create_unique_constraint(
        "uq_platform_menu_config",
        "admin_menu_configs",
        ["platform_id", "menu_item_id"],
    )
    op.create_unique_constraint(
        "uq_user_menu_config",
        "admin_menu_configs",
        ["user_id", "menu_item_id"],
    )

    # Drop frontend_type index
    op.drop_index("idx_admin_menu_configs_frontend_type", table_name="admin_menu_configs")

    # Drop frontend_type column
    op.drop_column("admin_menu_configs", "frontend_type")

    # Drop the enum type
    sa.Enum("admin", "vendor", name="frontendtype").drop(op.get_bind(), checkfirst=True)
