"""Add deleted_at column to core tables that don't have it yet.

Revision ID: 0012_soft_delete
Revises: 0011_attachments
Create Date: 2026-07-04
"""

from __future__ import annotations

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = "0012_soft_delete"
down_revision: Union[str, None] = "0011_attachments"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

# Core tables that need deleted_at added
# Excludes: audit_log (immutable), deletion_log (already has it),
# tenants (top-level), user_tenants (join table with cascade delete)
TABLES_NEEDING_SOFT_DELETE = [
    "users",
    "roles",
    "sessions",
    "notifications",
    "ai_conversations",
    "ai_messages",
    "workflows",
    "workflow_instances",
    "workflow_step_history",
    "company_contacts",
]


def upgrade() -> None:
    for table_name in TABLES_NEEDING_SOFT_DELETE:
        op.add_column(
            table_name,
            sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        )


def downgrade() -> None:
    for table_name in reversed(TABLES_NEEDING_SOFT_DELETE):
        op.drop_column(table_name, "deleted_at")
