-- ADR-090-E rollback: remove Ansible learning writeback receipt from operation_type allowlist.
-- Only apply after confirming no automation_operation_log rows use the operation type.

DO $$
BEGIN
    IF EXISTS (
        SELECT 1
        FROM automation_operation_log
        WHERE operation_type = 'ansible_learning_writeback_recorded'
        LIMIT 1
    ) THEN
        RAISE EXCEPTION 'cannot remove ansible_learning_writeback_recorded while rows still exist';
    END IF;
END $$;

ALTER TABLE automation_operation_log
    DROP CONSTRAINT IF EXISTS automation_operation_log_type_valid;

ALTER TABLE automation_operation_log
    ADD CONSTRAINT automation_operation_log_type_valid CHECK (operation_type IN (
        'monitor_configured','monitor_removed',
        'alert_fired','alert_suppressed','alert_routed',
        'rule_created','rule_updated','rule_matched','rule_rejected','rule_deprecated',
        'playbook_generated','playbook_updated','playbook_executed',
        'remediation_executed','remediation_verified','remediation_rolled_back',
        'self_correction_attempted',
        'km_created','km_updated','km_linked',
        'asset_discovered','coverage_recalculated',
        'capacity_recommendation','quota_enforced',
        'notification_formatted',
        'ansible_candidate_matched',
        'ansible_check_mode_executed',
        'ansible_apply_executed',
        'ansible_rollback_executed',
        'ansible_execution_skipped'
    ));
