-- Migration 015: AI Quality Metrics and Tests
-- Tracking AI system quality metrics and test results

CREATE TABLE IF NOT EXISTS compliance_quality_metrics (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id     UUID NOT NULL DEFAULT '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e',
    name          TEXT NOT NULL,
    category      TEXT NOT NULL DEFAULT 'accuracy',
    -- accuracy | fairness | robustness | explainability | performance
    score         NUMERIC(5,2) NOT NULL DEFAULT 0,
    threshold     NUMERIC(5,2) NOT NULL DEFAULT 80,
    trend         TEXT DEFAULT 'stable',
    -- up | down | stable
    ai_system     TEXT,
    last_measured TIMESTAMPTZ DEFAULT NOW(),
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at    TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_quality_metrics_tenant ON compliance_quality_metrics(tenant_id);

CREATE TABLE IF NOT EXISTS compliance_quality_tests (
    id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id   UUID NOT NULL DEFAULT '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e',
    name        TEXT NOT NULL,
    status      TEXT NOT NULL DEFAULT 'pending',
    -- passed | failed | warning | pending
    duration    TEXT,
    ai_system   TEXT,
    details     TEXT,
    last_run    TIMESTAMPTZ DEFAULT NOW(),
    created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_quality_tests_tenant ON compliance_quality_tests(tenant_id);
