-- EL PANEL VE LAS ESTACIONES Y LA COLA (2026-07-29)
-- Dos acciones mas para el panel de la imprenta: quien esta reportando y que
-- venta se quedo trancada. Sin esto, el latido no le sirve de nada a Gilberto.
begin;

create or replace function public.fn_panel_imprenta(p_accion text, p_datos jsonb default '{}'::jsonb)
returns jsonb
language plpgsql
security definer
set search_path to 'public', 'fiscal'
as $fn$
declare v jsonb;
begin
  case p_accion
    when 'entrar' then
      select to_jsonb(t) into v from fiscal.entrar_panel(p_datos->>'usuario', p_datos->>'clave') t;
    when 'tablero' then
      select to_jsonb(t) into v from fiscal.tablero() t;
    when 'clientes' then
      select coalesce(jsonb_agg(to_jsonb(t)), '[]'::jsonb) into v from fiscal.clientes() t;
    when 'guardar_cliente' then
      select to_jsonb(fiscal.guardar_cliente(
        p_datos->>'rif', p_datos->>'razon_social', p_datos->>'domicilio',
        p_datos->>'representante', p_datos->>'cedula', p_datos->>'correo',
        p_datos->>'telefono', p_datos->>'sistema', coalesce(p_datos->>'serie', 'A'),
        coalesce((p_datos->>'rif_comprobado')::boolean, false), p_datos->>'nota')) into v;
    when 'activar_cliente' then
      select to_jsonb(fiscal.activar_cliente(p_datos->>'rif', (p_datos->>'activo')::boolean)) into v;
    when 'llaves' then
      select coalesce(jsonb_agg(to_jsonb(t)), '[]'::jsonb) into v from fiscal.llaves_de(p_datos->>'rif') t;
    when 'crear_llave' then
      select to_jsonb(fiscal.crear_llave(p_datos->>'rif', coalesce(p_datos->>'nombre', 'llave'))) into v;
    when 'revocar_llave' then
      select to_jsonb(fiscal.revocar_llave((p_datos->>'id')::uuid)) into v;
    when 'resumen_mensual' then
      select to_jsonb(t) into v from fiscal.resumen_mensual(
        p_datos->>'rif', (p_datos->>'anio')::int, (p_datos->>'mes')::int) t;
    when 'relacion_mensual' then
      select coalesce(jsonb_agg(to_jsonb(t)), '[]'::jsonb) into v from fiscal.relacion_mensual(
        p_datos->>'rif', (p_datos->>'anio')::int, (p_datos->>'mes')::int) t;
    when 'marcar_enviado' then
      select to_jsonb(fiscal.marcar_enviado(
        p_datos->>'rif', (p_datos->>'anio')::int, (p_datos->>'mes')::int,
        p_datos->>'quien', p_datos->>'nota')) into v;
    when 'negocios_gustito' then
      select coalesce(jsonb_agg(to_jsonb(t)), '[]'::jsonb) into v from fiscal.negocios_gustito() t;
    when 'buscar_negocio' then
      select coalesce(jsonb_agg(to_jsonb(t)), '[]'::jsonb) into v from fiscal.buscar_negocio(p_datos->>'texto') t;
    when 'vincular_negocio' then
      select fiscal.vincular_negocio((p_datos->>'restaurante_id')::uuid, p_datos->>'rif',
                                     p_datos->>'serie', p_datos->>'quien', p_datos->>'nota') into v;
    when 'desvincular_negocio' then
      select to_jsonb(fiscal.desvincular_negocio((p_datos->>'restaurante_id')::uuid)) into v;
    -- Quien esta reportando y quien lleva horas callado.
    when 'estaciones' then
      select coalesce(jsonb_agg(to_jsonb(t)), '[]'::jsonb) into v from fiscal.estaciones_vivas() t;
    -- Ventas que no se han podido sellar.
    when 'cola' then
      select coalesce(jsonb_agg(to_jsonb(t)), '[]'::jsonb) into v from fiscal.cola_por_revisar() t;
    else
      raise exception 'Accion desconocida';
  end case;
  return coalesce(v, 'null'::jsonb);
end;
$fn$;

revoke execute on function public.fn_panel_imprenta(text, jsonb) from anon, authenticated, public;
grant execute on function public.fn_panel_imprenta(text, jsonb) to service_role;

commit;
