-- Score Snapshots: Historical compliance score tracking
-- Migration 050

CREATE TABLE IF NOT EXISTS compliance_score_snapshots (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id UUID NOT NULL,
    project_id UUID,
    score DECIMAL(5,2) NOT NULL,
    controls_total INTEGER DEFAULT 0,
    controls_pass INTEGER DEFAULT 0,
    controls_partial INTEGER DEFAULT 0,
    evidence_total INTEGER DEFAULT 0,
    evidence_valid INTEGER DEFAULT 0,
    risks_total INTEGER DEFAULT 0,
    risks_high INTEGER DEFAULT 0,
    snapshot_date DATE NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    UNIQUE (tenant_id, project_id, snapshot_date)
);

CREATE INDEX IF NOT EXISTS idx_score_snap_tenant ON compliance_score_snapshots(tenant_id);
CREATE INDEX IF NOT EXISTS idx_score_snap_date ON compliance_score_snapshots(snapshot_date);
