-- Chain Directives: Structured specification documents that drive chain creation and evaluation
-- Contract Evaluations: LLM evaluation results for completed contracts

-- =============================================================================
-- New Tables
-- =============================================================================

-- Chain directives: formal specification documents for directive-driven chains
CREATE TABLE IF NOT EXISTS chain_directives (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    chain_id UUID NOT NULL REFERENCES chains(id) ON DELETE CASCADE,
    version INTEGER NOT NULL DEFAULT 1,

    -- Directive content (structured JSON)
    -- Requirements: [{ id, title, description, priority, category, parentId? }]
    requirements JSONB NOT NULL DEFAULT '[]',
    -- Acceptance criteria: [{ id, requirementIds[], description, testable, verificationMethod }]
    acceptance_criteria JSONB NOT NULL DEFAULT '[]',
    -- Constraints: [{ id, type, description, impact }]
    constraints JSONB NOT NULL DEFAULT '[]',
    -- External dependencies: [{ id, name, type, status, requiredBy[] }]
    external_dependencies JSONB NOT NULL DEFAULT '[]',

    -- Metadata
    source_type VARCHAR(32) NOT NULL DEFAULT 'llm_generated',  -- 'manual', 'llm_generated', 'imported'

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    CONSTRAINT chain_directives_chain_unique UNIQUE (chain_id)
);

CREATE INDEX IF NOT EXISTS idx_chain_directives_chain_id ON chain_directives(chain_id);

-- Contract evaluations: LLM evaluation results after contract completion
CREATE TABLE IF NOT EXISTS contract_evaluations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    contract_id UUID NOT NULL REFERENCES contracts(id) ON DELETE CASCADE,
    chain_id UUID REFERENCES chains(id) ON DELETE SET NULL,
    chain_contract_id UUID REFERENCES chain_contracts(id) ON DELETE SET NULL,

    -- Evaluation metadata
    evaluation_number INTEGER NOT NULL DEFAULT 1,
    evaluator_model VARCHAR(100),

    -- Results
    passed BOOLEAN NOT NULL,
    overall_score DECIMAL(3,2),  -- 0.00 to 1.00

    -- Structured feedback
    -- criteria_results: [{ criterionId, criterionText, passed, score, feedback, evidence[] }]
    criteria_results JSONB NOT NULL DEFAULT '[]',
    summary_feedback TEXT NOT NULL,
    rework_instructions TEXT,

    -- Context snapshot for reproducibility
    directive_snapshot JSONB,
    deliverables_snapshot JSONB,

    started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    completed_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_contract_evaluations_contract ON contract_evaluations(contract_id);
CREATE INDEX IF NOT EXISTS idx_contract_evaluations_chain ON contract_evaluations(chain_id);
CREATE INDEX IF NOT EXISTS idx_contract_evaluations_chain_contract ON contract_evaluations(chain_contract_id);

-- =============================================================================
-- Table Modifications
-- =============================================================================

-- Extend chains table with directive support
ALTER TABLE chains
    ADD COLUMN IF NOT EXISTS directive_contract_id UUID REFERENCES contracts(id) ON DELETE SET NULL,
    ADD COLUMN IF NOT EXISTS directive_document TEXT,
    ADD COLUMN IF NOT EXISTS evaluation_enabled BOOLEAN NOT NULL DEFAULT true,
    ADD COLUMN IF NOT EXISTS default_pass_threshold DECIMAL(3,2) DEFAULT 0.80,
    ADD COLUMN IF NOT EXISTS default_max_retries INTEGER DEFAULT 3;

CREATE INDEX IF NOT EXISTS idx_chains_directive_contract ON chains(directive_contract_id) WHERE directive_contract_id IS NOT NULL;

-- Extend contracts table with chain directive support
ALTER TABLE contracts
    ADD COLUMN IF NOT EXISTS spawned_chain_id UUID REFERENCES chains(id) ON DELETE SET NULL,
    ADD COLUMN IF NOT EXISTS is_chain_directive BOOLEAN NOT NULL DEFAULT FALSE;

CREATE INDEX IF NOT EXISTS idx_contracts_spawned_chain ON contracts(spawned_chain_id) WHERE spawned_chain_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_contracts_is_directive ON contracts(is_chain_directive) WHERE is_chain_directive = true;

-- Extend chain_contracts table with evaluation tracking
ALTER TABLE chain_contracts
    ADD COLUMN IF NOT EXISTS evaluation_status VARCHAR(50) NOT NULL DEFAULT 'pending',
    ADD COLUMN IF NOT EXISTS evaluation_retry_count INTEGER NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS max_evaluation_retries INTEGER NOT NULL DEFAULT 3,
    ADD COLUMN IF NOT EXISTS last_evaluation_id UUID REFERENCES contract_evaluations(id) ON DELETE SET NULL,
    ADD COLUMN IF NOT EXISTS rework_feedback TEXT,
    ADD COLUMN IF NOT EXISTS rework_started_at TIMESTAMPTZ,
    ADD COLUMN IF NOT EXISTS original_completion_at TIMESTAMPTZ;

-- Extend chain_contract_definitions table with requirement traceability
ALTER TABLE chain_contract_definitions
    ADD COLUMN IF NOT EXISTS requirement_ids TEXT[] DEFAULT '{}',
    ADD COLUMN IF NOT EXISTS acceptance_criteria JSONB DEFAULT '[]',
    ADD COLUMN IF NOT EXISTS evaluation_enabled BOOLEAN NOT NULL DEFAULT true,
    ADD COLUMN IF NOT EXISTS pass_threshold DECIMAL(3,2) DEFAULT 0.80;

-- =============================================================================
-- Comments
-- =============================================================================

COMMENT ON TABLE chain_directives IS 'Formal directive documents that drive chain creation and contract evaluation';
COMMENT ON COLUMN chain_directives.requirements IS 'Enumerated requirements with IDs for traceability';
COMMENT ON COLUMN chain_directives.acceptance_criteria IS 'Testable criteria mapped to requirements';
COMMENT ON COLUMN chain_directives.constraints IS 'Technical, business, time, or resource constraints';
COMMENT ON COLUMN chain_directives.external_dependencies IS 'External APIs, services, or data dependencies';

COMMENT ON TABLE contract_evaluations IS 'LLM evaluation results after contract completion within a chain';
COMMENT ON COLUMN contract_evaluations.criteria_results IS 'Per-criterion evaluation results with scores and feedback';
COMMENT ON COLUMN contract_evaluations.directive_snapshot IS 'Snapshot of directive at evaluation time for audit';

COMMENT ON COLUMN chains.directive_contract_id IS 'Reference to the directive contract that created/orchestrates this chain';
COMMENT ON COLUMN chains.evaluation_enabled IS 'Whether LLM evaluation is enabled after contract completion';

COMMENT ON COLUMN contracts.spawned_chain_id IS 'Reference to chain spawned by this directive contract';
COMMENT ON COLUMN contracts.is_chain_directive IS 'Whether this contract is a chain directive orchestrator';

COMMENT ON COLUMN chain_contracts.evaluation_status IS 'Evaluation state: pending, evaluating, passed, failed, rework, escalated';
COMMENT ON COLUMN chain_contracts.evaluation_retry_count IS 'Number of evaluation retry attempts';
