"""fc2c-iii-a: series_page ordered membership

Revision ID: 0005
Revises: 0004
Create Date: 2026-05-16

"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "0005"
down_revision: Union[str, None] = "0004"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "series_page",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("series_tag_id", sa.Integer(), nullable=False),
        sa.Column("image_id", sa.Integer(), nullable=False),
        sa.Column("page_number", sa.Integer(), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(timezone=True),
            nullable=False, server_default=sa.func.now(),
        ),
        sa.Column(
            "updated_at", sa.DateTime(timezone=True),
            nullable=False, server_default=sa.func.now(),
        ),
        sa.ForeignKeyConstraint(
            ["series_tag_id"], ["tag.id"], ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(
            ["image_id"], ["image_record.id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("image_id", name="uq_series_page_image"),
    )
    op.create_index(
        "ix_series_page_series_tag_id", "series_page", ["series_tag_id"]
    )


def downgrade() -> None:
    op.drop_index("ix_series_page_series_tag_id", table_name="series_page")
    op.drop_table("series_page")
