alter table public.order_groups add column if not exists delivery_date date;
alter table public.order_groups add column if not exists delivery_time text;
alter table public.order_groups add column if not exists notification_status text not null default 'not_started';

create index if not exists idx_order_groups_delivery_status
on public.order_groups (delivery_status);

create index if not exists idx_order_groups_notification_status
on public.order_groups (notification_status, updated_at);

alter table public.order_groups enable row level security;

drop policy if exists "order groups select by role" on public.order_groups;
create policy "order groups select by role" on public.order_groups
for select
using (true);

drop policy if exists "order groups update coordination roles" on public.order_groups;
create policy "order groups update coordination roles" on public.order_groups
for update
using (public.current_role_name() in ('manager', 'logistician', 'admin'))
with check (public.current_role_name() in ('manager', 'logistician', 'admin'));

drop policy if exists "order groups insert service roles" on public.order_groups;
create policy "order groups insert service roles" on public.order_groups
for insert
with check (public.current_role_name() in ('manager', 'logistician', 'admin'));
