create table if not exists public.bays (
  id         uuid primary key default gen_random_uuid(),
  name       text not null,
  active     boolean not null default true,
  sort_order integer not null default 0,
  notes      text
);

insert into public.bays (name, sort_order)
select 'Bay ' || n, n from generate_series(1, 2) n
where not exists (select 1 from public.bays);

alter table public.services
  add column if not exists buffer_before_minutes integer not null default 0  check (buffer_before_minutes >= 0),
  add column if not exists buffer_after_minutes  integer not null default 15 check (buffer_after_minutes >= 0);

alter table public.booking_policy
  add column if not exists default_buffer_minutes integer not null default 15,
  add column if not exists slot_interval_minutes  integer not null default 30;

alter table public.bookings
  add column if not exists bay_id uuid references public.bays(id) on delete set null;

create index if not exists bookings_bay_date_idx on public.bookings (bay_id, booking_date);

alter table public.bays enable row level security;
drop policy if exists "Public can read bays" on public.bays;
create policy "Public can read bays"
  on public.bays for select to anon, authenticated using (true);
