"""add target owners and alert notification events

Revision ID: 0005_target_owners
Revises: 0004_email_settings
Create Date: 2026-02-12
"""

from alembic import op
import sqlalchemy as sa


revision = "0005_target_owners"
down_revision = "0004_email_settings"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "target_owners",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("target_id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("assigned_by_user_id", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.ForeignKeyConstraint(["assigned_by_user_id"], ["users.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(["target_id"], ["targets.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("target_id", "user_id", name="uq_target_owner_target_user"),
    )
    op.create_index(op.f("ix_target_owners_target_id"), "target_owners", ["target_id"], unique=False)
    op.create_index(op.f("ix_target_owners_user_id"), "target_owners", ["user_id"], unique=False)

    op.create_table(
        "alert_notification_events",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("alert_key", sa.String(length=200), nullable=False),
        sa.Column("target_id", sa.Integer(), nullable=False),
        sa.Column("severity", sa.String(length=16), nullable=False),
        sa.Column("last_seen_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("last_sent_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.ForeignKeyConstraint(["target_id"], ["targets.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("alert_key", "target_id", "severity", name="uq_alert_notif_event_key_target_sev"),
    )
    op.create_index(op.f("ix_alert_notification_events_alert_key"), "alert_notification_events", ["alert_key"], unique=False)
    op.create_index(op.f("ix_alert_notification_events_target_id"), "alert_notification_events", ["target_id"], unique=False)


def downgrade() -> None:
    op.drop_index(op.f("ix_alert_notification_events_target_id"), table_name="alert_notification_events")
    op.drop_index(op.f("ix_alert_notification_events_alert_key"), table_name="alert_notification_events")
    op.drop_table("alert_notification_events")

    op.drop_index(op.f("ix_target_owners_user_id"), table_name="target_owners")
    op.drop_index(op.f("ix_target_owners_target_id"), table_name="target_owners")
    op.drop_table("target_owners")
