-- =============================================
-- Migration: Template Fixes
-- 1. RPC increment_template_use_count
-- 2. Pastikan RLS DELETE policy ada
-- Run AFTER schema.sql
-- =============================================

-- ── RPC: increment use_count secara atomic ───────────────────────────────────
CREATE OR REPLACE FUNCTION increment_template_use_count(p_template_id UUID)
RETURNS VOID
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
BEGIN
  UPDATE prompt_templates
  SET use_count = use_count + 1,
      updated_at = NOW()
  WHERE id = p_template_id;
END;
$$;
