CREATE OR REPLACE FUNCTION public.fn_admin_create_email_template(p_template_key character varying, p_language character varying, p_subject character varying, p_body_html text, p_body_text text)
 RETURNS jsonb
 LANGUAGE plpgsql
AS $function$
BEGIN
    INSERT INTO public.email_templates (template_key, language, subject, body_html, body_text, active)
    VALUES (p_template_key, p_language, p_subject, p_body_html, p_body_text, true);
    RETURN jsonb_build_object('success', true);
EXCEPTION WHEN unique_violation THEN
    RETURN jsonb_build_object('error', 'Ya existe una plantilla con ese identificador e idioma.');
END;
$function$
;
