-- Migration: 001_create_agents
-- Creates the agents table for the Agent Registry service.

CREATE TABLE IF NOT EXISTS agents (
  agent_id        UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  email           VARCHAR(255) NOT NULL UNIQUE,
  agent_type      VARCHAR(32)  NOT NULL,
  version         VARCHAR(64)  NOT NULL,
  capabilities    TEXT[]       NOT NULL DEFAULT '{}',
  owner           VARCHAR(128) NOT NULL,
  deployment_env  VARCHAR(16)  NOT NULL,
  status          VARCHAR(24)  NOT NULL DEFAULT 'active',
  created_at      TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
  updated_at      TIMESTAMPTZ  NOT NULL DEFAULT NOW(),

  CONSTRAINT agents_agent_type_check
    CHECK (agent_type IN ('screener','classifier','orchestrator','extractor','summarizer','router','monitor','custom')),
  CONSTRAINT agents_deployment_env_check
    CHECK (deployment_env IN ('development','staging','production')),
  CONSTRAINT agents_status_check
    CHECK (status IN ('active','suspended','decommissioned'))
);

CREATE INDEX IF NOT EXISTS idx_agents_email     ON agents (email);
CREATE INDEX IF NOT EXISTS idx_agents_status    ON agents (status);
CREATE INDEX IF NOT EXISTS idx_agents_owner     ON agents (owner);
CREATE INDEX IF NOT EXISTS idx_agents_agent_type ON agents (agent_type);
CREATE INDEX IF NOT EXISTS idx_agents_created_at ON agents (created_at DESC);
