-- Migration 017: Loeschfristen (Retention Policies)
-- Full retention policy management with legal holds and storage locations

CREATE TABLE IF NOT EXISTS compliance_loeschfristen (
    id                      UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id               UUID NOT NULL DEFAULT '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e',
    policy_id               TEXT,                              -- "LF-2026-001"
    data_object_name        TEXT NOT NULL,
    description             TEXT,
    affected_groups         JSONB DEFAULT '[]'::jsonb,
    data_categories         JSONB DEFAULT '[]'::jsonb,
    primary_purpose         TEXT,
    deletion_trigger        TEXT NOT NULL DEFAULT 'PURPOSE_END',
    -- PURPOSE_END | RETENTION_DRIVER | LEGAL_HOLD
    retention_driver        TEXT,
    -- AO_147 | HGB_257 | USTG_14B | BGB_195 | ARBZG_16 | AGG_15 | BDSG_35 | BSIG | CUSTOM
    retention_driver_detail TEXT,
    retention_duration      INTEGER,
    retention_unit          TEXT,                              -- DAYS | MONTHS | YEARS
    retention_description   TEXT,
    start_event             TEXT,
    has_active_legal_hold   BOOLEAN DEFAULT FALSE,
    legal_holds             JSONB DEFAULT '[]'::jsonb,
    storage_locations       JSONB DEFAULT '[]'::jsonb,
    deletion_method         TEXT DEFAULT 'MANUAL_REVIEW_DELETE',
    deletion_method_detail  TEXT,
    responsible_role        TEXT,
    responsible_person      TEXT,
    release_process         TEXT,
    linked_vvt_activity_ids JSONB DEFAULT '[]'::jsonb,
    status                  TEXT NOT NULL DEFAULT 'DRAFT',
    -- DRAFT | ACTIVE | REVIEW_NEEDED | ARCHIVED
    last_review_date        TIMESTAMPTZ,
    next_review_date        TIMESTAMPTZ,
    review_interval         TEXT DEFAULT 'ANNUAL',
    -- QUARTERLY | SEMI_ANNUAL | ANNUAL
    tags                    JSONB DEFAULT '[]'::jsonb,
    created_at              TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at              TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_loeschfristen_tenant  ON compliance_loeschfristen(tenant_id);
CREATE INDEX IF NOT EXISTS idx_loeschfristen_status  ON compliance_loeschfristen(status);
CREATE INDEX IF NOT EXISTS idx_loeschfristen_driver  ON compliance_loeschfristen(retention_driver);
CREATE INDEX IF NOT EXISTS idx_loeschfristen_review  ON compliance_loeschfristen(next_review_date) WHERE next_review_date IS NOT NULL;
