-- Re-create agent_prompts table to ensure correct schema (with updated_at)
DROP TABLE IF EXISTS agent_prompts CASCADE;

CREATE TABLE 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 ux_agent_prompts_agent_kind_version ON agent_prompts(agent_id, kind, version);
CREATE INDEX ix_agent_prompts_agent_id ON agent_prompts(agent_id);
CREATE INDEX ix_agent_prompts_agent_kind_active ON agent_prompts(agent_id, kind) WHERE is_active = true;

-- Grant permissions
GRANT ALL ON agent_prompts TO postgres;
