-- Notification system tables
-- Stores notification targets and delivery attempts

CREATE TABLE notification_targets (
    id UUID PRIMARY KEY,
    org_id UUID NOT NULL REFERENCES orgs(id),
    name TEXT NOT NULL,
    target_type TEXT NOT NULL CHECK (target_type IN ('webhook', 'email', 'slack')),
    webhook_url TEXT,
    enabled BOOLEAN NOT NULL DEFAULT true,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX idx_notification_targets_org ON notification_targets(org_id);

CREATE TABLE notification_attempts (
    id UUID PRIMARY KEY,
    incident_id UUID NOT NULL REFERENCES incidents(id),
    target_id UUID NOT NULL REFERENCES notification_targets(id),
    status TEXT NOT NULL CHECK (status IN ('pending', 'sent', 'failed')),
    error TEXT,
    sent_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE (incident_id, target_id)
);
