-- Migration 037: Document Versioning Tables
-- Separate version tables for DSFA, VVT, TOM, Loeschfristen, Obligations
-- Pattern: snapshot JSONB + status workflow + audit trail

BEGIN;

-- ============================================================================
-- 1. Add current_version column to all 5 document tables
-- ============================================================================

ALTER TABLE compliance_dsfas
    ADD COLUMN IF NOT EXISTS current_version INTEGER DEFAULT 0;

ALTER TABLE compliance_vvt_activities
    ADD COLUMN IF NOT EXISTS current_version INTEGER DEFAULT 0;

ALTER TABLE compliance_tom_measures
    ADD COLUMN IF NOT EXISTS current_version INTEGER DEFAULT 0;

ALTER TABLE compliance_loeschfristen
    ADD COLUMN IF NOT EXISTS current_version INTEGER DEFAULT 0;

ALTER TABLE compliance_obligations
    ADD COLUMN IF NOT EXISTS current_version INTEGER DEFAULT 0;

-- ============================================================================
-- 2. DSFA Versions
-- ============================================================================

CREATE TABLE IF NOT EXISTS compliance_dsfa_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    dsfa_id UUID NOT NULL,
    tenant_id VARCHAR(255) NOT NULL,
    version_number INTEGER NOT NULL,
    status VARCHAR(20) DEFAULT 'draft',
    snapshot JSONB NOT NULL,
    change_summary TEXT,
    changed_sections JSONB DEFAULT '[]'::jsonb,
    created_by VARCHAR(200) DEFAULT 'system',
    approved_by VARCHAR(200),
    approved_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (dsfa_id, version_number)
);

CREATE INDEX IF NOT EXISTS idx_dsfa_versions_dsfa ON compliance_dsfa_versions(dsfa_id);
CREATE INDEX IF NOT EXISTS idx_dsfa_versions_tenant ON compliance_dsfa_versions(tenant_id);

-- ============================================================================
-- 3. VVT Activity Versions
-- ============================================================================

CREATE TABLE IF NOT EXISTS compliance_vvt_activity_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    activity_id UUID NOT NULL,
    tenant_id VARCHAR(255) NOT NULL,
    version_number INTEGER NOT NULL,
    status VARCHAR(20) DEFAULT 'draft',
    snapshot JSONB NOT NULL,
    change_summary TEXT,
    changed_sections JSONB DEFAULT '[]'::jsonb,
    created_by VARCHAR(200) DEFAULT 'system',
    approved_by VARCHAR(200),
    approved_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (activity_id, version_number)
);

CREATE INDEX IF NOT EXISTS idx_vvt_activity_versions_activity ON compliance_vvt_activity_versions(activity_id);
CREATE INDEX IF NOT EXISTS idx_vvt_activity_versions_tenant ON compliance_vvt_activity_versions(tenant_id);

-- ============================================================================
-- 4. TOM Versions
-- ============================================================================

CREATE TABLE IF NOT EXISTS compliance_tom_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    measure_id UUID NOT NULL,
    tenant_id VARCHAR(255) NOT NULL,
    version_number INTEGER NOT NULL,
    status VARCHAR(20) DEFAULT 'draft',
    snapshot JSONB NOT NULL,
    change_summary TEXT,
    changed_sections JSONB DEFAULT '[]'::jsonb,
    created_by VARCHAR(200) DEFAULT 'system',
    approved_by VARCHAR(200),
    approved_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (measure_id, version_number)
);

CREATE INDEX IF NOT EXISTS idx_tom_versions_measure ON compliance_tom_versions(measure_id);
CREATE INDEX IF NOT EXISTS idx_tom_versions_tenant ON compliance_tom_versions(tenant_id);

-- ============================================================================
-- 5. Loeschfristen Versions
-- ============================================================================

CREATE TABLE IF NOT EXISTS compliance_loeschfristen_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    policy_id UUID NOT NULL,
    tenant_id VARCHAR(255) NOT NULL,
    version_number INTEGER NOT NULL,
    status VARCHAR(20) DEFAULT 'draft',
    snapshot JSONB NOT NULL,
    change_summary TEXT,
    changed_sections JSONB DEFAULT '[]'::jsonb,
    created_by VARCHAR(200) DEFAULT 'system',
    approved_by VARCHAR(200),
    approved_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (policy_id, version_number)
);

CREATE INDEX IF NOT EXISTS idx_loeschfristen_versions_policy ON compliance_loeschfristen_versions(policy_id);
CREATE INDEX IF NOT EXISTS idx_loeschfristen_versions_tenant ON compliance_loeschfristen_versions(tenant_id);

-- ============================================================================
-- 6. Obligation Versions
-- ============================================================================

CREATE TABLE IF NOT EXISTS compliance_obligation_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    obligation_id UUID NOT NULL,
    tenant_id VARCHAR(255) NOT NULL,
    version_number INTEGER NOT NULL,
    status VARCHAR(20) DEFAULT 'draft',
    snapshot JSONB NOT NULL,
    change_summary TEXT,
    changed_sections JSONB DEFAULT '[]'::jsonb,
    created_by VARCHAR(200) DEFAULT 'system',
    approved_by VARCHAR(200),
    approved_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE (obligation_id, version_number)
);

CREATE INDEX IF NOT EXISTS idx_obligation_versions_obligation ON compliance_obligation_versions(obligation_id);
CREATE INDEX IF NOT EXISTS idx_obligation_versions_tenant ON compliance_obligation_versions(tenant_id);

COMMIT;
