/*
 * -------------------------------------------------------
 * Municipality/Events Schema (Ferienpass)
 * Phase 7: events, registrations, holiday passes
 * -------------------------------------------------------
 */

create table if not exists public.events (
  id uuid primary key default gen_random_uuid(),
  account_id uuid not null references public.accounts(id) on delete cascade,
  name text not null,
  description text,
  event_date date not null,
  event_time time,
  end_date date,
  location text,
  capacity integer,
  min_age integer,
  max_age integer,
  fee numeric(10,2) default 0,
  status text not null default 'planned' check (status in ('planned', 'open', 'full', 'running', 'completed', 'cancelled')),
  registration_deadline date,
  contact_name text,
  contact_email text,
  contact_phone text,
  custom_data jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);
create index ix_events_account on public.events(account_id);
create index ix_events_date on public.events(account_id, event_date);
alter table public.events enable row level security;
revoke all on public.events from authenticated, service_role;
grant select, insert, update, delete on public.events to authenticated;
grant all on public.events to service_role;
create policy events_select on public.events for select to authenticated using (public.has_role_on_account(account_id));
create policy events_mutate on public.events for all to authenticated using (public.has_permission(auth.uid(), account_id, 'modules.write'::public.app_permissions));
create trigger trg_events_updated_at before update on public.events for each row execute function public.update_account_settings_timestamp();

create table if not exists public.event_registrations (
  id uuid primary key default gen_random_uuid(),
  event_id uuid not null references public.events(id) on delete cascade,
  first_name text not null,
  last_name text not null,
  email text,
  phone text,
  date_of_birth date,
  parent_name text,
  parent_phone text,
  status text not null default 'confirmed' check (status in ('pending', 'confirmed', 'waitlisted', 'cancelled')),
  notes text,
  created_at timestamptz not null default now()
);
create index ix_event_registrations_event on public.event_registrations(event_id);
alter table public.event_registrations enable row level security;
revoke all on public.event_registrations from authenticated, service_role;
grant select, insert, update, delete on public.event_registrations to authenticated;
grant all on public.event_registrations to service_role;
create policy event_registrations_select on public.event_registrations for select to authenticated using (exists (select 1 from public.events e where e.id = event_registrations.event_id and public.has_role_on_account(e.account_id)));
create policy event_registrations_mutate on public.event_registrations for all to authenticated using (exists (select 1 from public.events e where e.id = event_registrations.event_id and public.has_permission(auth.uid(), e.account_id, 'modules.write'::public.app_permissions)));

-- Holiday passes (Ferienpass)
create table if not exists public.holiday_passes (
  id uuid primary key default gen_random_uuid(),
  account_id uuid not null references public.accounts(id) on delete cascade,
  name text not null,
  year integer not null,
  description text,
  price numeric(10,2) default 0,
  valid_from date,
  valid_until date,
  created_at timestamptz not null default now()
);
create index ix_holiday_passes_account on public.holiday_passes(account_id);
alter table public.holiday_passes enable row level security;
revoke all on public.holiday_passes from authenticated, service_role;
grant select, insert, update, delete on public.holiday_passes to authenticated;
grant all on public.holiday_passes to service_role;
create policy holiday_passes_select on public.holiday_passes for select to authenticated using (public.has_role_on_account(account_id));
create policy holiday_passes_mutate on public.holiday_passes for all to authenticated using (public.has_permission(auth.uid(), account_id, 'modules.write'::public.app_permissions));

create table if not exists public.holiday_pass_activities (
  id uuid primary key default gen_random_uuid(),
  pass_id uuid not null references public.holiday_passes(id) on delete cascade,
  event_id uuid references public.events(id) on delete set null,
  name text not null,
  description text,
  activity_date date,
  capacity integer,
  created_at timestamptz not null default now()
);
create index ix_holiday_pass_activities_pass on public.holiday_pass_activities(pass_id);
alter table public.holiday_pass_activities enable row level security;
revoke all on public.holiday_pass_activities from authenticated, service_role;
grant select, insert, update, delete on public.holiday_pass_activities to authenticated;
grant all on public.holiday_pass_activities to service_role;
create policy holiday_pass_activities_select on public.holiday_pass_activities for select to authenticated using (exists (select 1 from public.holiday_passes hp where hp.id = holiday_pass_activities.pass_id and public.has_role_on_account(hp.account_id)));
create policy holiday_pass_activities_mutate on public.holiday_pass_activities for all to authenticated using (exists (select 1 from public.holiday_passes hp where hp.id = holiday_pass_activities.pass_id and public.has_permission(auth.uid(), hp.account_id, 'modules.write'::public.app_permissions)));
