"""Create sequences table.

Revision ID: 0009_sequences
Revises: 0008_tax_rates
Create Date: 2026-07-04
"""

from __future__ import annotations

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision: str = "0009_sequences"
down_revision: Union[str, None] = "0008_tax_rates"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "sequences",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("name", sa.String(100), nullable=False),
        sa.Column("prefix", sa.String(20), nullable=False, server_default=""),
        sa.Column("next_number", sa.Integer, nullable=False, server_default="1"),
        sa.Column("padding", sa.Integer, nullable=False, server_default="4"),
        sa.Column("tenant_id", postgresql.UUID(as_uuid=True), nullable=False, index=True),
        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.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.create_index("ix_sequences_tenant_name", "sequences", ["tenant_id", "name"])


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