-- ============================================================
-- Gustito Express — Flujo de mesa: metodo "punto de venta" +
-- color del restaurante en el seguimiento y el ticket del cliente.
-- ============================================================

-- 1) Permitir 'punto_venta' como metodo de pago (datafono en la mesa)
alter table public.pedidos drop constraint if exists pedidos_metodo_pago_check;
alter table public.pedidos add constraint pedidos_metodo_pago_check
  check (metodo_pago = any (array['pago_movil','efectivo','transferencia','zelle','punto_venta']));
alter table public.pagos drop constraint if exists pagos_metodo_check;
alter table public.pagos add constraint pagos_metodo_check
  check (metodo = any (array['pago_movil','efectivo','transferencia','zelle','punto_venta']));

-- 2) pedido_seguimiento: agregar el color del restaurante
drop function if exists public.pedido_seguimiento(uuid);
create function public.pedido_seguimiento(p_pedido_id uuid)
returns table(
  codigo text, estado text, tipo_entrega text, total_usd numeric, tasa_bs numeric,
  restaurante_nombre text, restaurante_telefono text, restaurante_color text,
  direccion text, direccion_latitud double precision, direccion_longitud double precision,
  repartidor_latitud double precision, repartidor_longitud double precision,
  repartidor_ubicacion_en timestamp with time zone,
  recibido_en timestamp with time zone, confirmado_en timestamp with time zone,
  listo_en timestamp with time zone, en_camino_en timestamp with time zone,
  entregado_en timestamp with time zone, tiempo_estimado_min int
)
language sql stable security definer set search_path to 'public'
as $$
  select p.codigo, p.estado, p.tipo_entrega, p.total_usd, p.tasa_bs,
         r.nombre, r.telefono, r.color_primario,
         p.direccion, p.direccion_latitud, p.direccion_longitud,
         rep.ultima_latitud, rep.ultima_longitud, rep.ultima_ubicacion_en,
         p.recibido_en, p.confirmado_en, p.listo_en, p.en_camino_en, p.entregado_en,
         p.tiempo_estimado_min
    from public.pedidos p
    join public.restaurantes r on r.id = p.restaurante_id
    left join public.repartidores rep on rep.id = p.repartidor_id
   where p.id = p_pedido_id;
$$;
grant execute on function public.pedido_seguimiento(uuid) to anon, authenticated;

-- 3) pedido_ticket: agregar el color del restaurante
drop function if exists public.pedido_ticket(uuid);
create function public.pedido_ticket(p_pedido_id uuid)
returns table(
  codigo text, estado text, tipo_entrega text, mesa text,
  cliente_nombre text, cliente_telefono text, cliente_cedula text,
  total_usd numeric, tasa_bs numeric, pago_estado text, metodo_pago text,
  restaurante_nombre text, restaurante_color text, items jsonb
)
language sql stable security definer set search_path to 'public'
as $$
  select p.codigo, p.estado, p.tipo_entrega, p.mesa,
         p.cliente_nombre, p.cliente_telefono, p.cliente_cedula,
         p.total_usd, p.tasa_bs, pg.estado, p.metodo_pago,
         r.nombre, r.color_primario,
         coalesce(
           (select jsonb_agg(jsonb_build_object(
                     'nombre', it.nombre, 'cantidad', it.cantidad,
                     'precio_usd', it.precio_usd, 'nota', it.nota))
              from public.pedido_items it where it.pedido_id = p.id),
           '[]'::jsonb)
    from public.pedidos p
    join public.restaurantes r on r.id = p.restaurante_id
    left join public.pagos pg on pg.pedido_id = p.id
   where p.id = p_pedido_id
   limit 1;
$$;
grant execute on function public.pedido_ticket(uuid) to anon, authenticated;
