-- Migration: Add directory fields to agents table
-- Adds: urlKey, description, specialties, chainOfCommand for agent directory feature

ALTER TABLE agents ADD COLUMN url_key text;
ALTER TABLE agents ADD COLUMN description text;
ALTER TABLE agents ADD COLUMN specialties jsonb DEFAULT '[]'::jsonb;
ALTER TABLE agents ADD COLUMN chain_of_command jsonb;

-- url_key should match name pattern and be unique (nullable for backward compat)
ALTER TABLE agents ADD CONSTRAINT agents_url_key_check
  CHECK (url_key IS NULL OR url_key ~ '^[a-z0-9][a-z0-9-]{0,63}$');

-- Create unique index on url_key (partial index to allow NULLs)
CREATE UNIQUE INDEX agents_url_key_idx ON agents(url_key) WHERE url_key IS NOT NULL;

-- Description max length
ALTER TABLE agents ADD CONSTRAINT agents_description_check
  CHECK (description IS NULL OR length(description) BETWEEN 1 AND 2048);

-- Update existing agents to use name as url_key
UPDATE agents SET url_key = name WHERE url_key IS NULL;
