"""refactor

迁移 ID: 8b619d7fca40
父迁移: a62db4039f17
创建时间: 2025-12-26 22:28:41.568551

"""

from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "8b619d7fca40"
down_revision: str | Sequence[str] | None = "a62db4039f17"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade(name: str = "") -> None:
    if name:
        return
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "lp_member_permission",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("member_id", sa.String(length=255), nullable=False),
        sa.Column("type", sa.String(length=255), nullable=False),
        sa.Column("permissions", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_lp_member_permission")),
        sa.UniqueConstraint(
            "member_id", "type", name="uq_lp_member_permission_member_id_type"
        ),
        info={"bind_key": "perm"},
    )
    with op.batch_alter_table("lp_member_permission", schema=None) as batch_op:
        batch_op.create_index(
            "idx_lp_member_permission_member_id_type",
            ["member_id", "type"],
            unique=False,
        )

    op.create_table(
        "lp_permission_group",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("group_name", sa.String(length=255), nullable=False),
        sa.Column("permissions", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_lp_permission_group")),
        sa.UniqueConstraint("group_name", name="uq_lp_permission_group_group_name"),
        info={"bind_key": "perm"},
    )
    with op.batch_alter_table("lp_permission_group", schema=None) as batch_op:
        batch_op.create_index(
            "idx_lp_permission_group_group_name", ["group_name"], unique=False
        )

    op.create_table(
        "lp_member_to_permission_group",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("member_id", sa.String(length=255), nullable=False),
        sa.Column("member_type", sa.String(length=255), nullable=False),
        sa.Column("group_name", sa.String(length=255), nullable=False),
        sa.ForeignKeyConstraint(
            ["group_name"],
            ["lp_permission_group.group_name"],
            name=op.f(
                "fk_lp_member_to_permission_group_group_name_lp_permission_group"
            ),
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["member_id"],
            ["lp_member_permission.member_id"],
            name=op.f(
                "fk_lp_member_to_permission_group_member_id_lp_member_permission"
            ),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_lp_member_to_permission_group")),
        sa.UniqueConstraint(
            "member_id",
            "group_name",
            "member_type",
            name="uq_lp_member_to_permission_group_and_member",
        ),
        info={"bind_key": "perm"},
    )
    with op.batch_alter_table("lp_member_to_permission_group", schema=None) as batch_op:
        batch_op.create_index(
            "idx_lp_member_to_permission_group_group_name", ["group_name"], unique=False
        )
        batch_op.create_index(
            "idx_lp_member_to_permission_group_member_id", ["member_id"], unique=False
        )
        batch_op.create_index(
            "idx_lp_member_to_permission_group_member_type",
            ["member_type"],
            unique=False,
        )

    # ### end Alembic commands ###


def downgrade(name: str = "") -> None:
    if name:
        return
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("lp_member_to_permission_group", schema=None) as batch_op:
        batch_op.drop_index("idx_lp_member_to_permission_group_member_type")
        batch_op.drop_index("idx_lp_member_to_permission_group_member_id")
        batch_op.drop_index("idx_lp_member_to_permission_group_group_name")

    op.drop_table("lp_member_to_permission_group")
    with op.batch_alter_table("lp_permission_group", schema=None) as batch_op:
        batch_op.drop_index("idx_lp_permission_group_group_name")

    op.drop_table("lp_permission_group")
    with op.batch_alter_table("lp_member_permission", schema=None) as batch_op:
        batch_op.drop_index("idx_lp_member_permission_member_id_type")

    op.drop_table("lp_member_permission")
    # ### end Alembic commands ###
