"""add case attachments

Revision ID: 9a001807d27b
Revises: 4c1a69e5b34b
Create Date: 2025-07-02 11:30:09.686449

"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "9a001807d27b"
down_revision: str | None = "4c1a69e5b34b"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Add new enum values first, before creating tables that might reference them
    # NOTE: We accidentally added these values in a previous migration,
    # then manually rewrote the migration history, so we need to ignore errors
    try:
        op.execute("ALTER TYPE caseeventtype ADD VALUE 'ATTACHMENT_CREATED'")
        op.execute("ALTER TYPE caseeventtype ADD VALUE 'ATTACHMENT_DELETED'")
    except Exception:
        pass

    op.create_table(
        "file",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", sa.UUID(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("sha256", sa.String(length=64), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("content_type", sa.String(length=100), nullable=False),
        sa.Column("size", sa.Integer(), nullable=False),
        sa.Column("creator_id", sa.UUID(), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint("surrogate_id", name="file_pkey"),
    )
    op.create_index(op.f("ix_file_id"), "file", ["id"], unique=True)
    op.create_index(op.f("ix_file_sha256"), "file", ["sha256"], unique=False)
    op.create_table(
        "case_attachment",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("case_id", sa.UUID(), nullable=False),
        sa.Column("file_id", sa.UUID(), nullable=False),
        sa.ForeignKeyConstraint(
            ["case_id"],
            ["cases.id"],
            ondelete="CASCADE",
            name="case_attachment_case_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["file_id"],
            ["file.id"],
            ondelete="CASCADE",
            name="case_attachment_file_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="case_attachment_pkey"),
        sa.UniqueConstraint("case_id", "file_id", name="uq_case_attachment_case_file"),
    )
    op.create_index(
        op.f("ix_case_attachment_id"), "case_attachment", ["id"], unique=True
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Note: PostgreSQL doesn't support removing enum values directly
    # The enum values will remain but won't be used after dropping the tables
    op.drop_index(op.f("ix_case_attachment_id"), table_name="case_attachment")
    op.drop_table("case_attachment")
    op.drop_index(op.f("ix_file_sha256"), table_name="file")
    op.drop_index(op.f("ix_file_id"), table_name="file")
    op.drop_table("file")
    # ### end Alembic commands ###
