-- Super Admin AI prompt templates (per website / domain), with public share ids. CREATE TABLE IF NOT EXISTS ai_prompt_templates ( id BIGSERIAL PRIMARY KEY, domain_id BIGINT NOT NULL REFERENCES domains (id) ON DELETE CASCADE, name VARCHAR(255) NOT NULL, body TEXT NOT NULL, public_id VARCHAR(32) NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), CONSTRAINT ai_prompt_templates_public_id_unique UNIQUE (public_id) ); CREATE INDEX IF NOT EXISTS idx_ai_prompt_templates_domain_id ON ai_prompt_templates (domain_id); CREATE INDEX IF NOT EXISTS idx_ai_prompt_templates_created_at ON ai_prompt_templates (created_at DESC); CREATE OR REPLACE FUNCTION ai_prompt_templates_set_updated_at() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at = now(); RETURN NEW; END; $$ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS ai_prompt_templates_set_updated_at ON ai_prompt_templates; CREATE TRIGGER ai_prompt_templates_set_updated_at BEFORE UPDATE ON ai_prompt_templates FOR EACH ROW EXECUTE FUNCTION ai_prompt_templates_set_updated_at();