"""Add user_change_logs table for audit trail

Revision ID: 008_add_user_change_log
Revises: 007_add_role_check_constraint
Create Date: 2026-02-22

"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

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


def upgrade() -> None:
    op.create_table(
        "user_change_logs",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("user_id", sa.dialects.postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id"), nullable=False),
        sa.Column("changed_by_id", sa.dialects.postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id"), nullable=True),
        sa.Column("change_type", sa.String(50), nullable=False),
        sa.Column("field_changed", sa.String(50), nullable=True),
        sa.Column("old_value", sa.String(255), nullable=True),
        sa.Column("new_value", sa.String(255), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("ix_user_change_logs_user_id", "user_change_logs", ["user_id"])
    op.create_index("ix_user_change_logs_created_at", "user_change_logs", ["created_at"])


def downgrade() -> None:
    op.drop_index("ix_user_change_logs_created_at", table_name="user_change_logs")
    op.drop_index("ix_user_change_logs_user_id", table_name="user_change_logs")
    op.drop_table("user_change_logs")
