"""Notification center — add target_user_id, read_at, notification to audit_log

Revision ID: 021
Revises: 020
Create Date: 2026-03-03
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID

revision = "021"
down_revision = "020"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "audit_log",
        sa.Column(
            "target_user_id",
            UUID(as_uuid=True),
            sa.ForeignKey("users.id", ondelete="SET NULL"),
            nullable=True,
        ),
    )
    op.add_column(
        "audit_log",
        sa.Column("read_at", sa.DateTime(), nullable=True),
    )
    op.add_column(
        "audit_log",
        sa.Column(
            "notification",
            sa.Boolean(),
            nullable=False,
            server_default=sa.text("false"),
        ),
    )

    # Composite index for user notification queries
    op.create_index(
        "ix_audit_log_target_notification",
        "audit_log",
        ["target_user_id", "notification", "read_at"],
    )

    # Partial index for listing recent notifications
    op.create_index(
        "ix_audit_log_notification_ts",
        "audit_log",
        ["notification", "timestamp"],
        postgresql_where=sa.text("notification = true"),
    )


def downgrade() -> None:
    op.drop_index("ix_audit_log_notification_ts", table_name="audit_log")
    op.drop_index("ix_audit_log_target_notification", table_name="audit_log")
    op.drop_column("audit_log", "notification")
    op.drop_column("audit_log", "read_at")
    op.drop_column("audit_log", "target_user_id")
