"""initial schema

Revision ID: bf37d72775bc
Revises:
Create Date: 2025-06-25 14:55:14.394624

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "bf37d72775bc"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "topics",
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("id", sa.Integer(), sa.Identity(always=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("CURRENT_TIMESTAMP"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("CURRENT_TIMESTAMP"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "wal_files",
        sa.Column("s3_uri", sa.Text(), nullable=False),
        sa.Column("s3_etag", sa.Text(), nullable=True),
        sa.Column("total_bytes", sa.BigInteger(), nullable=False),
        sa.Column("total_messages", sa.Integer(), nullable=False),
        sa.Column("compacted", sa.Boolean(), nullable=False),
        sa.Column("compacted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("compacted_by", sa.Text(), nullable=True),
        sa.Column("iceberg_commit_id", sa.Text(), nullable=True),
        sa.Column("id", sa.BigInteger(), sa.Identity(always=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("CURRENT_TIMESTAMP"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("CURRENT_TIMESTAMP"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "partitions",
        sa.Column("topic_name", sa.String(), nullable=False),
        sa.Column("partition_number", sa.Integer(), nullable=False),
        sa.Column("last_offset", sa.BigInteger(), nullable=False),
        sa.Column("id", sa.Integer(), sa.Identity(always=True), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("CURRENT_TIMESTAMP"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("CURRENT_TIMESTAMP"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(["topic_name"], ["topics.name"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("topic_name", "partition_number"),
    )
    op.create_index(
        "ix_partition_topic_partition",
        "partitions",
        ["topic_name", "partition_number"],
        unique=False,
    )
    op.create_table(
        "wal_file_offsets",
        sa.Column("wal_file_id", sa.BigInteger(), nullable=False),
        sa.Column("partition_id", sa.Integer(), nullable=False),
        sa.Column("base_offset", sa.BigInteger(), nullable=False),
        sa.Column("last_offset", sa.BigInteger(), nullable=False),
        sa.Column("id", sa.BigInteger(), sa.Identity(always=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["partition_id"], ["partitions.id"], ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(["wal_file_id"], ["wal_files.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("wal_file_offsets")
    op.drop_index("ix_partition_topic_partition", table_name="partitions")
    op.drop_table("partitions")
    op.drop_table("wal_files")
    op.drop_table("topics")
    # ### end Alembic commands ###
