-- ============================================================
-- Gustito Express — El repartidor tambien necesita navegar al NEGOCIO (retiro).
-- Ampliamos mi_perfil_repartidor con la ubicacion del restaurante.
-- ============================================================
drop function if exists public.mi_perfil_repartidor();
create or replace function public.mi_perfil_repartidor()
returns table (
  repartidor_id        uuid,
  estado               text,
  restaurante_id       uuid,
  restaurante_nombre   text,
  restaurante_latitud  double precision,
  restaurante_longitud double precision,
  restaurante_direccion text
)
language sql stable security definer set search_path = public
as $$
  select r.id, r.estado, r.restaurante_id, res.nombre, res.latitud, res.longitud, res.direccion
  from public.repartidores r
  join public.restaurantes res on res.id = r.restaurante_id
  where r.perfil_id = auth.uid();
$$;
grant execute on function public.mi_perfil_repartidor() to authenticated;
