-- Migration 013: Compliance Obligations Tracking
-- Standalone obligation items (DSGVO/AI-Act Pflichten-Verwaltung)
-- Derived from UCCA assessments or created manually

SET search_path TO compliance, core, public;

CREATE TABLE IF NOT EXISTS compliance_obligations (
    id              UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id       UUID NOT NULL DEFAULT '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e',
    title           TEXT NOT NULL,
    description     TEXT,
    source          TEXT NOT NULL DEFAULT 'DSGVO',   -- 'DSGVO', 'AI Act', 'NIS2', etc.
    source_article  TEXT,                            -- e.g. 'Art. 35', 'Art. 9'
    deadline        TIMESTAMPTZ,
    status          TEXT NOT NULL DEFAULT 'pending', -- pending|in-progress|completed|overdue
    priority        TEXT NOT NULL DEFAULT 'medium',  -- critical|high|medium|low
    responsible     TEXT,
    linked_systems  JSONB DEFAULT '[]'::jsonb,
    -- Link to UCCA assessment (if auto-derived)
    assessment_id   UUID,
    rule_code       TEXT,                            -- UCCA rule code if derived
    notes           TEXT,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at      TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_obligations_tenant    ON compliance_obligations(tenant_id);
CREATE INDEX IF NOT EXISTS idx_obligations_status    ON compliance_obligations(status);
CREATE INDEX IF NOT EXISTS idx_obligations_priority  ON compliance_obligations(priority);
CREATE INDEX IF NOT EXISTS idx_obligations_deadline  ON compliance_obligations(deadline) WHERE deadline IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_obligations_created   ON compliance_obligations(created_at DESC);
