-- Create agent_prompts table
CREATE TABLE IF NOT EXISTS agent_prompts (
    id text PRIMARY KEY DEFAULT ('ap_' || substr(md5(random()::text), 1, 12)),
    agent_id text NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
    kind text NOT NULL CHECK (kind IN ('core', 'safety', 'governance', 'tools')),
    content text NOT NULL,
    version integer NOT NULL DEFAULT 1,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW(),
    created_by text,
    note text,
    is_active boolean NOT NULL DEFAULT true
);

-- Create indexes
CREATE UNIQUE INDEX IF NOT EXISTS ux_agent_prompts_agent_kind_version ON agent_prompts(agent_id, kind, version);
CREATE INDEX IF NOT EXISTS ix_agent_prompts_agent_id ON agent_prompts(agent_id);
CREATE INDEX IF NOT EXISTS ix_agent_prompts_agent_kind_active ON agent_prompts(agent_id, kind) WHERE is_active = true;

-- Grant permissions (adjust based on your RBAC)
GRANT ALL ON agent_prompts TO postgres;
-- GRANT SELECT, INSERT, UPDATE ON agent_prompts TO app_user; -- Uncomment if needed

