-- V1 Control Enrichment: Cross-reference table for matching
-- Eigenentwicklung (v1, ungrouped, no source) → regulatorische Controls

CREATE TABLE IF NOT EXISTS v1_control_matches (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    v1_control_uuid UUID NOT NULL REFERENCES canonical_controls(id) ON DELETE CASCADE,
    matched_control_uuid UUID NOT NULL REFERENCES canonical_controls(id) ON DELETE CASCADE,
    similarity_score NUMERIC(4,3) NOT NULL,
    match_rank SMALLINT NOT NULL DEFAULT 1,
    matched_source TEXT,           -- e.g. "DSGVO (EU) 2016/679"
    matched_article TEXT,          -- e.g. "Art. 32"
    match_method VARCHAR(30) NOT NULL DEFAULT 'embedding',
    created_at TIMESTAMPTZ DEFAULT NOW(),
    CONSTRAINT uq_v1_match UNIQUE (v1_control_uuid, matched_control_uuid)
);

CREATE INDEX IF NOT EXISTS idx_v1m_v1 ON v1_control_matches(v1_control_uuid);
CREATE INDEX IF NOT EXISTS idx_v1m_matched ON v1_control_matches(matched_control_uuid);
