-- IHF Phase 11 — Advanced AI Federation (IHUB-WP-0012)
-- Migration timestamp: 1744156800

-- agent_registrations: named, versioned AI agents backed by llm-connect providers
CREATE TABLE agent_registrations (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    hub_id UUID NOT NULL REFERENCES hubs(id),
    name TEXT NOT NULL,
    slug TEXT NOT NULL UNIQUE,
    description TEXT,
    provider TEXT NOT NULL,
    model_name TEXT NOT NULL,
    trust_level TEXT NOT NULL DEFAULT 'advisory',
    capabilities JSONB NOT NULL DEFAULT '[]',
    system_prompt TEXT,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    version INTEGER NOT NULL DEFAULT 1,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    CHECK (trust_level IN ('advisory', 'elevated', 'autonomous'))
);

CREATE INDEX agent_registrations_hub_id_idx ON agent_registrations (hub_id);
CREATE INDEX agent_registrations_slug_idx ON agent_registrations (slug);
CREATE INDEX agent_registrations_is_active_idx ON agent_registrations (is_active);

-- model_routing_policies: task_type → agent selection rules per hub
CREATE TABLE model_routing_policies (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    hub_id UUID NOT NULL REFERENCES hubs(id),
    task_type TEXT NOT NULL,
    agent_registration_id UUID NOT NULL REFERENCES agent_registrations(id),
    priority INTEGER NOT NULL DEFAULT 0,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    UNIQUE (hub_id, task_type, priority)
);

CREATE INDEX model_routing_policies_hub_task_idx ON model_routing_policies (hub_id, task_type);

-- agent_delegations: auditable inter-agent subtask delegation records
CREATE TABLE agent_delegations (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    delegating_agent_id UUID NOT NULL REFERENCES agent_registrations(id),
    receiving_agent_id UUID NOT NULL REFERENCES agent_registrations(id),
    parent_proposal_id UUID REFERENCES agent_proposals(id),
    scope TEXT NOT NULL,
    token_budget INTEGER NOT NULL DEFAULT 1000,
    tokens_used INTEGER,
    status TEXT NOT NULL DEFAULT 'pending',
    result JSONB,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    completed_at TIMESTAMP WITH TIME ZONE,
    CHECK (status IN ('pending', 'completed', 'failed', 'cancelled'))
);

CREATE INDEX agent_delegations_delegating_idx ON agent_delegations (delegating_agent_id);
CREATE INDEX agent_delegations_receiving_idx ON agent_delegations (receiving_agent_id);
CREATE INDEX agent_delegations_parent_proposal_idx ON agent_delegations (parent_proposal_id);

-- collective_proposals: multi-agent proposals with attribution
CREATE TABLE collective_proposals (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    title TEXT NOT NULL,
    summary TEXT,
    task_type TEXT NOT NULL,
    consensus_status TEXT NOT NULL DEFAULT 'pending',
    final_content JSONB,
    source_widget_id UUID REFERENCES widgets(id),
    source_candidate_id UUID REFERENCES requirement_candidates(id),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    CHECK (consensus_status IN ('pending', 'consensus', 'divergent'))
);

CREATE INDEX collective_proposals_task_type_idx ON collective_proposals (task_type);
CREATE INDEX collective_proposals_consensus_status_idx ON collective_proposals (consensus_status);

-- collective_proposal_contributions: per-agent contribution records
CREATE TABLE collective_proposal_contributions (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    collective_proposal_id UUID NOT NULL REFERENCES collective_proposals(id),
    agent_registration_id UUID NOT NULL REFERENCES agent_registrations(id),
    content JSONB NOT NULL,
    tokens_in INTEGER,
    tokens_out INTEGER,
    model_used TEXT,
    contributed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);

CREATE INDEX collective_proposal_contributions_proposal_idx ON collective_proposal_contributions (collective_proposal_id);
CREATE INDEX collective_proposal_contributions_agent_idx ON collective_proposal_contributions (agent_registration_id);

-- ai_governance_policies: per-hub rules controlling agent scope
CREATE TABLE ai_governance_policies (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    hub_id UUID NOT NULL REFERENCES hubs(id),
    agent_registration_id UUID NOT NULL REFERENCES agent_registrations(id),
    artifact_type TEXT NOT NULL,
    allowed_actions JSONB NOT NULL DEFAULT '["read"]',
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);

CREATE INDEX ai_governance_policies_hub_agent_idx ON ai_governance_policies (hub_id, agent_registration_id);
CREATE INDEX ai_governance_policies_is_active_idx ON ai_governance_policies (is_active);

-- agent_performance_records: periodic snapshots of per-agent metrics
CREATE TABLE agent_performance_records (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
    agent_registration_id UUID NOT NULL REFERENCES agent_registrations(id),
    hub_id UUID NOT NULL REFERENCES hubs(id),
    period_start TIMESTAMP WITH TIME ZONE NOT NULL,
    period_end TIMESTAMP WITH TIME ZONE NOT NULL,
    proposals_generated INTEGER NOT NULL DEFAULT 0,
    proposals_accepted INTEGER NOT NULL DEFAULT 0,
    proposals_rejected INTEGER NOT NULL DEFAULT 0,
    proposals_revised INTEGER NOT NULL DEFAULT 0,
    mean_confidence DOUBLE PRECISION,
    calibration_score DOUBLE PRECISION,
    computed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);

CREATE INDEX agent_performance_records_agent_idx ON agent_performance_records (agent_registration_id);
CREATE INDEX agent_performance_records_period_idx ON agent_performance_records (period_start, period_end);

-- Extend agent_proposals with agent_registration_id and token tracking
ALTER TABLE agent_proposals
    ADD COLUMN agent_registration_id UUID REFERENCES agent_registrations(id),
    ADD COLUMN tokens_in INTEGER,
    ADD COLUMN tokens_out INTEGER;

CREATE INDEX agent_proposals_agent_registration_idx ON agent_proposals (agent_registration_id);
