"""add eval_monitors table and monitor_id to eval_runs

Revision ID: fb266e680e34
Revises: 1b151bbba793
Create Date: 2026-03-12 16:34:26.605612
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = 'fb266e680e34'
down_revision: Union[str, None] = '1b151bbba793'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('eval_monitors',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('project_id', sa.UUID(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('target_type', sa.String(length=20), nullable=False),
    sa.Column('metric_names', postgresql.ARRAY(sa.String(length=255)), nullable=False),
    sa.Column('filters', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.Column('sampling_rate', sa.Float(), nullable=False),
    sa.Column('model', sa.String(length=255), nullable=True),
    sa.Column('cadence', sa.String(length=50), nullable=False),
    sa.Column('only_if_changed', sa.Boolean(), nullable=False),
    sa.Column('status', sa.Enum('ACTIVE', 'PAUSED', name='monitor_status', native_enum=False, length=20), nullable=False),
    sa.Column('last_run_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('last_run_id', sa.UUID(), nullable=True),
    sa.Column('next_run_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['last_run_id'], ['eval_runs.id'], name='fk_eval_monitors_last_run_id', ondelete='SET NULL', use_alter=True),
    sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_eval_monitors_next_run', 'eval_monitors', ['status', 'next_run_at'], unique=False)
    op.create_index('ix_eval_monitors_project', 'eval_monitors', ['project_id', 'status'], unique=False)
    op.add_column('eval_runs', sa.Column('monitor_id', sa.UUID(), nullable=True))
    op.create_foreign_key(None, 'eval_runs', 'eval_monitors', ['monitor_id'], ['id'], ondelete='SET NULL')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'eval_runs', type_='foreignkey')
    op.drop_column('eval_runs', 'monitor_id')
    op.drop_index('ix_eval_monitors_project', table_name='eval_monitors')
    op.drop_index('ix_eval_monitors_next_run', table_name='eval_monitors')
    op.drop_table('eval_monitors')
    # ### end Alembic commands ###
