-- V17 — Alerts: drop ACKNOWLEDGED state, add read_at/deleted_at, drop alert_reads,
-- rework open-rule unique index predicate to survive ack (acked no longer "closed").

-- 1. Coerce ACKNOWLEDGED rows → FIRING (acked_at already set on these rows)
UPDATE alert_instances SET state = 'FIRING' WHERE state = 'ACKNOWLEDGED';

-- 2. Swap alert_state_enum to remove ACKNOWLEDGED (Postgres can't drop enum values in place)
--    First drop all indexes that reference alert_state_enum so ALTER COLUMN can proceed.
DROP INDEX IF EXISTS alert_instances_open_rule_uq;
DROP INDEX IF EXISTS alert_instances_inbox_idx;
DROP INDEX IF EXISTS alert_instances_open_rule_idx;
DROP INDEX IF EXISTS alert_instances_resolved_idx;

CREATE TYPE alert_state_enum_v2 AS ENUM ('PENDING','FIRING','RESOLVED');
ALTER TABLE alert_instances
  ALTER COLUMN state TYPE alert_state_enum_v2
  USING state::text::alert_state_enum_v2;
DROP TYPE alert_state_enum;
ALTER TYPE alert_state_enum_v2 RENAME TO alert_state_enum;

-- Recreate the non-unique indexes that were dropped above
CREATE INDEX alert_instances_inbox_idx     ON alert_instances (environment_id, state, fired_at DESC);
CREATE INDEX alert_instances_open_rule_idx ON alert_instances (rule_id, state) WHERE rule_id IS NOT NULL;
CREATE INDEX alert_instances_resolved_idx  ON alert_instances (resolved_at) WHERE state = 'RESOLVED';

-- 3. New orthogonal flag columns
ALTER TABLE alert_instances
  ADD COLUMN read_at    timestamptz NULL,
  ADD COLUMN deleted_at timestamptz NULL;

CREATE INDEX alert_instances_unread_idx
  ON alert_instances (environment_id, read_at)
  WHERE read_at IS NULL AND deleted_at IS NULL;

CREATE INDEX alert_instances_deleted_idx
  ON alert_instances (deleted_at)
  WHERE deleted_at IS NOT NULL;

-- 4. Rework the V13/V15/V16 open-rule uniqueness index:
--    - drop ACKNOWLEDGED from the predicate (ack no longer "closes")
--    - add "AND deleted_at IS NULL" so a soft-deleted row frees the slot
DROP INDEX IF EXISTS alert_instances_open_rule_uq;
CREATE UNIQUE INDEX alert_instances_open_rule_uq
  ON alert_instances (rule_id, (COALESCE(
        context->>'_subjectFingerprint',
        context->'exchange'->>'id',
        '')))
  WHERE rule_id IS NOT NULL
    AND state IN ('PENDING','FIRING')
    AND deleted_at IS NULL;

-- 5. Drop the per-user reads table — read is now global on alert_instances.read_at
DROP TABLE alert_reads;
