-- Create trust metrics and data quality tables
-- Migration: 010_create_trust_tables.up.sql

-- Trust metrics table for storing trust and quality scores
CREATE TABLE IF NOT EXISTS trust_metrics (
    id TEXT PRIMARY KEY,
    organization_id TEXT NOT NULL,
    metric_type VARCHAR(50) NOT NULL, -- organization, resource_flow, site
    metric_name VARCHAR(100) NOT NULL,
    value DECIMAL(5,4) CHECK (value >= 0 AND value <= 1), -- 0.0 to 1.0
    score VARCHAR(20), -- excellent, good, fair, poor
    evidence JSONB,
    last_updated TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    valid_until TIMESTAMP WITH TIME ZONE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Create indexes for trust_metrics
CREATE INDEX IF NOT EXISTS idx_trust_metrics_org_id ON trust_metrics(organization_id);
CREATE INDEX IF NOT EXISTS idx_trust_metrics_type ON trust_metrics(metric_type);
CREATE INDEX IF NOT EXISTS idx_trust_metrics_name ON trust_metrics(metric_name);
CREATE INDEX IF NOT EXISTS idx_trust_metrics_updated ON trust_metrics(last_updated);

-- Verified data table for tracking verification status
CREATE TABLE IF NOT EXISTS verified_data (
    id TEXT PRIMARY KEY,
    organization_id TEXT NOT NULL,
    data_type VARCHAR(50) NOT NULL, -- organization, resource_flow, site
    data_id TEXT NOT NULL,
    status VARCHAR(20) DEFAULT 'unverified', -- unverified, pending, verified, rejected
    verified_by TEXT,
    verified_at TIMESTAMP WITH TIME ZONE,
    evidence JSONB,
    expires_at TIMESTAMP WITH TIME ZONE,
    notes TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Create indexes for verified_data
CREATE INDEX IF NOT EXISTS idx_verified_data_org_id ON verified_data(organization_id);
CREATE INDEX IF NOT EXISTS idx_verified_data_status ON verified_data(status);
CREATE INDEX IF NOT EXISTS idx_verified_data_verified_at ON verified_data(verified_at);
CREATE INDEX IF NOT EXISTS idx_verified_data_expires_at ON verified_data(expires_at);

-- Historical success table for tracking performance metrics
CREATE TABLE IF NOT EXISTS historical_success (
    id TEXT PRIMARY KEY,
    organization_id TEXT NOT NULL,
    match_id TEXT,
    metric_type VARCHAR(50) NOT NULL, -- completion_rate, satisfaction, volume
    value DECIMAL(5,4) CHECK (value >= 0 AND value <= 1), -- 0.0 to 1.0
    period VARCHAR(20), -- monthly, quarterly, yearly
    recorded_at TIMESTAMP WITH TIME ZONE NOT NULL,
    data JSONB,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Create indexes for historical_success
CREATE INDEX IF NOT EXISTS idx_historical_success_org_id ON historical_success(organization_id);
CREATE INDEX IF NOT EXISTS idx_historical_success_match_id ON historical_success(match_id);
CREATE INDEX IF NOT EXISTS idx_historical_success_type ON historical_success(metric_type);
CREATE INDEX IF NOT EXISTS idx_historical_success_recorded_at ON historical_success(recorded_at);

-- Add foreign key constraints
ALTER TABLE trust_metrics ADD CONSTRAINT fk_trust_metrics_org
    FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE;

ALTER TABLE verified_data ADD CONSTRAINT fk_verified_data_org
    FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE;

ALTER TABLE historical_success ADD CONSTRAINT fk_historical_success_org
    FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE;

ALTER TABLE historical_success ADD CONSTRAINT fk_historical_success_match
    FOREIGN KEY (match_id) REFERENCES matches(id) ON DELETE SET NULL;
