"""your message

Revision ID: aa17f8a032c9
Revises: 8623aa4bf3a1
Create Date: 2026-06-01 11:31:16.648791
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def _has_column(table_name: str, column_name: str) -> bool:
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    columns = inspector.get_columns(table_name)
    return any(column.get("name") == column_name for column in columns)


def _has_index(table_name: str, index_name: str) -> bool:
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    indexes = inspector.get_indexes(table_name)
    return any(index.get("name") == index_name for index in indexes)


def _add_column_if_not_exists(table_name: str, column: sa.Column) -> None:
    if not _has_column(table_name, column.name):
        op.add_column(table_name, column)


def _drop_column_if_exists(table_name: str, column_name: str) -> None:
    if _has_column(table_name, column_name):
        op.drop_column(table_name, column_name)


def _create_index_if_not_exists(
    index_name: str,
    table_name: str,
    columns: list[str],
    unique: bool = False,
) -> None:
    if not _has_index(table_name, index_name):
        op.create_index(index_name, table_name, columns, unique=unique)


def _drop_index_if_exists(index_name: str, table_name: str) -> None:
    if _has_index(table_name, index_name):
        op.drop_index(index_name, table_name=table_name)


def upgrade() -> None:
    _add_column_if_not_exists(
        "chat_generation_tasks",
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    _create_index_if_not_exists(
        "ix_chat_generation_tasks_deleted_at",
        "chat_generation_tasks",
        ["deleted_at"],
        unique=False,
    )

    _add_column_if_not_exists(
        "generation_records",
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    _create_index_if_not_exists(
        "ix_generation_records_deleted_at",
        "generation_records",
        ["deleted_at"],
        unique=False,
    )

    _add_column_if_not_exists(
        "projects",
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    _create_index_if_not_exists(
        "ix_projects_deleted_at",
        "projects",
        ["deleted_at"],
        unique=False,
    )


def downgrade() -> None:
    _drop_index_if_exists("ix_projects_deleted_at", "projects")
    _drop_column_if_exists("projects", "deleted_at")

    _drop_index_if_exists("ix_generation_records_deleted_at", "generation_records")
    _drop_column_if_exists("generation_records", "deleted_at")

    _drop_index_if_exists(
        "ix_chat_generation_tasks_deleted_at",
        "chat_generation_tasks",
    )
    _drop_column_if_exists("chat_generation_tasks", "deleted_at")