-- Agent integration: collaboration matrix, handoffs, permissions

CREATE TABLE IF NOT EXISTS agent_collaboration (
    from_agent VARCHAR(64) NOT NULL,
    to_agent VARCHAR(64) NOT NULL,
    handoff_type VARCHAR(64) NOT NULL DEFAULT 'partner',
    description TEXT,
    PRIMARY KEY (from_agent, to_agent, handoff_type)
);

CREATE TABLE IF NOT EXISTS agent_handoffs (
    id SERIAL PRIMARY KEY,
    correlation_id UUID NOT NULL DEFAULT gen_random_uuid(),
    from_agent VARCHAR(64) NOT NULL,
    to_agent VARCHAR(64) NOT NULL,
    handoff_type VARCHAR(64) DEFAULT 'partner',
    payload JSONB DEFAULT '{}'::jsonb,
    status VARCHAR(32) NOT NULL DEFAULT 'pending',
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    completed_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_agent_handoffs_correlation ON agent_handoffs(correlation_id);
CREATE INDEX IF NOT EXISTS idx_agent_handoffs_pair ON agent_handoffs(from_agent, to_agent, created_at DESC);

-- Collaboration matrix (16 agents)
INSERT INTO agent_collaboration (from_agent, to_agent, handoff_type, description) VALUES
('marketing', 'retail', 'trends', 'RSS/trends naar retail kansen'),
('marketing', 'design', 'assets', 'Campagne assets'),
('marketing', 'knowledge', 'brand', 'Brand guidelines'),
('marketing', 'research', 'intel', 'Marktintel'),
('retail', 'bizdev', 'opportunities', 'Hot leads'),
('retail', 'finance', 'margin', 'SKU marge check'),
('retail', 'marketing', 'feedback', 'Campagne feedback'),
('bizdev', 'finance', 'valuation', 'Deal waardering'),
('bizdev', 'product', 'listing', 'Listing eisen'),
('bizdev', 'email', 'leads', 'Inbound leads'),
('finance', 'bizdev', 'margin_scenario', 'Margescenario'),
('finance', 'sourcing', 'costs', 'Inkoopkosten'),
('finance', 'retail', 'sku_margin', 'Retail SKU marge'),
('sourcing', 'product', 'suppliers', 'Leveranciers'),
('sourcing', 'finance', 'cost_data', 'Kostendata'),
('sourcing', 'halal', 'certs', 'Certificaat check'),
('product', 'halal', 'compliance', 'Ingrediënten compliance'),
('product', 'packaging', 'spec', 'SKU spec'),
('product', 'knowledge', 'recipes', 'Recepten NAS'),
('halal', 'product', 'feedback', 'Compliance feedback'),
('halal', 'packaging', 'claims', 'Label claims'),
('halal', 'sourcing', 'cert_docs', 'Cert documenten'),
('packaging', 'design', 'artwork', 'Label artwork'),
('packaging', 'product', 'spec', 'Product spec'),
('packaging', 'halal', 'claims', 'Halal claims op label'),
('design', 'marketing', 'campaign', 'Campagne visuals'),
('design', 'packaging', 'label', 'Label design'),
('design', 'product', 'visual', 'SKU visual'),
('knowledge', 'hr', 'handbooks', 'HR handboeken'),
('knowledge', 'product', 'docs', 'Product docs NAS'),
('knowledge', 'sysops', 'archive', 'Log archief'),
('research', 'marketing', 'intel', 'Marktintel marketing'),
('research', 'bizdev', 'intel', 'Concurrent intel'),
('research', 'retail', 'intel', 'Retail trends'),
('browser', 'research', 'scrape', 'Web scrape resultaten'),
('browser', 'sourcing', 'supplier_sites', 'Leverancier sites'),
('browser', 'retail', 'folder_scrape', 'Folder scrape'),
('email', 'bizdev', 'leads', 'Inbound leads'),
('email', 'hr', 'applications', 'Sollicitaties'),
('email', 'marketing', 'mentions', 'Mentions'),
('hr', 'sysops', 'onboarding', 'Account onboarding'),
('hr', 'knowledge', 'policy', 'HR beleid'),
('sysops', 'knowledge', 'logs', 'Infra logs'),
('sysops', 'hr', 'access', 'Toegang HR'),
('sysops', 'herman', 'infra_status', 'Infra status briefing')
ON CONFLICT (from_agent, to_agent, handoff_type) DO NOTHING;

-- Herman permissions: grant all modules for autonomous delegation
UPDATE herman_permissions SET granted = TRUE, granted_at = NOW(), updated_at = NOW()
WHERE granted IS NOT TRUE;

-- Auto-approve low-risk SysOps (stored in app_settings if table exists)
CREATE TABLE IF NOT EXISTS app_settings (
    key VARCHAR(128) PRIMARY KEY,
    value JSONB NOT NULL DEFAULT '{}'::jsonb,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

INSERT INTO app_settings (key, value) VALUES
('sysops_auto_approve', '{"maintenance_scan": true, "config_backup": true}'::jsonb)
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW();
