-- 15_bay_time_blocks.sql
-- Admin "block time" now blocks a BAY, not an agent. agent_time_blocks is left
-- in place (0 rows, harmless) but the admin calendar stops reading/writing it
-- once the bay-based rebuild ships — see bnm-booking-pivot-state memory.

create table if not exists public.bay_time_blocks (
  id         uuid primary key default gen_random_uuid(),
  bay_id     uuid not null references public.bays(id) on delete cascade,
  block_date date not null,
  start_time time not null,
  end_time   time not null,
  reason     text,
  created_at timestamptz not null default now(),
  constraint bay_time_blocks_time_order check (end_time > start_time)
);

create index if not exists bay_time_blocks_bay_date_idx
  on public.bay_time_blocks (bay_id, block_date);

alter table public.bay_time_blocks enable row level security;

drop policy if exists "Public can read bay time blocks" on public.bay_time_blocks;
create policy "Public can read bay time blocks"
  on public.bay_time_blocks for select to anon, authenticated using (true);
