-- Compliance Maximizer: Regulatory Optimization Engine
-- Stores optimization results with 3-zone analysis and compliant variants.

CREATE TABLE IF NOT EXISTS maximizer_optimizations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id UUID NOT NULL,

    -- Optional link to existing UCCA assessment
    assessment_id UUID,

    title VARCHAR(500) DEFAULT '',
    status VARCHAR(50) DEFAULT 'completed',

    -- Input
    input_config JSONB NOT NULL,
    input_intake JSONB,

    -- Results
    is_compliant BOOLEAN NOT NULL DEFAULT false,
    original_evaluation JSONB NOT NULL DEFAULT '{}',
    max_safe_config JSONB,
    variants JSONB DEFAULT '[]',
    zone_map JSONB DEFAULT '{}',

    -- Metadata
    constraint_version VARCHAR(50) DEFAULT '1.0.0',

    -- Audit
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    created_by UUID NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_maximizer_tenant
    ON maximizer_optimizations(tenant_id);

CREATE INDEX IF NOT EXISTS idx_maximizer_tenant_created
    ON maximizer_optimizations(tenant_id, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_maximizer_assessment
    ON maximizer_optimizations(assessment_id);
