CREATE OR REPLACE FUNCTION public.fn_admin_upsert_course_email_template(p_course_id integer, p_template_key text, p_language text, p_subject text, p_body_html text, p_body_text text)
 RETURNS void
 LANGUAGE sql
AS $function$
  INSERT INTO public.course_email_templates
    (course_id, template_key, language, subject, body_html, body_text, updated_at)
  VALUES
    (p_course_id, p_template_key, p_language, p_subject, p_body_html, p_body_text, now())
  ON CONFLICT (course_id, template_key, language) DO UPDATE SET
    subject    = EXCLUDED.subject,
    body_html  = EXCLUDED.body_html,
    body_text  = EXCLUDED.body_text,
    updated_at = now();
$function$
;
