/*
 * -------------------------------------------------------
 * Course Management Schema
 * Phase 5: courses, sessions, categories, participants,
 *          instructors, locations, attendance
 * -------------------------------------------------------
 */

create type public.enrollment_status as enum(
  'enrolled', 'waitlisted', 'cancelled', 'completed'
);

-- Course categories (hierarchical)
create table if not exists public.course_categories (
  id uuid primary key default gen_random_uuid(),
  account_id uuid not null references public.accounts(id) on delete cascade,
  parent_id uuid references public.course_categories(id) on delete set null,
  name text not null,
  description text,
  sort_order integer not null default 0,
  created_at timestamptz not null default now()
);
create index ix_course_categories_account on public.course_categories(account_id);
alter table public.course_categories enable row level security;
revoke all on public.course_categories from authenticated, service_role;
grant select, insert, update, delete on public.course_categories to authenticated;
grant all on public.course_categories to service_role;
create policy course_categories_select on public.course_categories for select to authenticated using (public.has_role_on_account(account_id));
create policy course_categories_mutate on public.course_categories for all to authenticated using (public.has_permission(auth.uid(), account_id, 'courses.write'::public.app_permissions));

-- Locations
create table if not exists public.course_locations (
  id uuid primary key default gen_random_uuid(),
  account_id uuid not null references public.accounts(id) on delete cascade,
  name text not null,
  address text,
  room text,
  capacity integer,
  notes text,
  created_at timestamptz not null default now()
);
create index ix_course_locations_account on public.course_locations(account_id);
alter table public.course_locations enable row level security;
revoke all on public.course_locations from authenticated, service_role;
grant select, insert, update, delete on public.course_locations to authenticated;
grant all on public.course_locations to service_role;
create policy course_locations_select on public.course_locations for select to authenticated using (public.has_role_on_account(account_id));
create policy course_locations_mutate on public.course_locations for all to authenticated using (public.has_permission(auth.uid(), account_id, 'courses.write'::public.app_permissions));

-- Instructors
create table if not exists public.course_instructors (
  id uuid primary key default gen_random_uuid(),
  account_id uuid not null references public.accounts(id) on delete cascade,
  first_name text not null,
  last_name text not null,
  email text,
  phone text,
  qualifications text,
  hourly_rate numeric(10,2),
  notes text,
  created_at timestamptz not null default now()
);
create index ix_course_instructors_account on public.course_instructors(account_id);
alter table public.course_instructors enable row level security;
revoke all on public.course_instructors from authenticated, service_role;
grant select, insert, update, delete on public.course_instructors to authenticated;
grant all on public.course_instructors to service_role;
create policy course_instructors_select on public.course_instructors for select to authenticated using (public.has_role_on_account(account_id));
create policy course_instructors_mutate on public.course_instructors for all to authenticated using (public.has_permission(auth.uid(), account_id, 'courses.write'::public.app_permissions));

-- Courses
create table if not exists public.courses (
  id uuid primary key default gen_random_uuid(),
  account_id uuid not null references public.accounts(id) on delete cascade,
  course_number text,
  name text not null,
  description text,
  category_id uuid references public.course_categories(id) on delete set null,
  instructor_id uuid references public.course_instructors(id) on delete set null,
  location_id uuid references public.course_locations(id) on delete set null,
  start_date date,
  end_date date,
  fee numeric(10,2) not null default 0,
  reduced_fee numeric(10,2),
  capacity integer not null default 20,
  min_participants integer default 5,
  status text not null default 'planned' check (status in ('planned', 'open', 'running', 'completed', 'cancelled')),
  registration_deadline date,
  notes 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_courses_account on public.courses(account_id);
create index ix_courses_status on public.courses(account_id, status);
create index ix_courses_dates on public.courses(account_id, start_date, end_date);
alter table public.courses enable row level security;
revoke all on public.courses from authenticated, service_role;
grant select, insert, update, delete on public.courses to authenticated;
grant all on public.courses to service_role;
create policy courses_select on public.courses for select to authenticated using (public.has_role_on_account(account_id));
create policy courses_insert on public.courses for insert to authenticated with check (public.has_permission(auth.uid(), account_id, 'courses.write'::public.app_permissions));
create policy courses_update on public.courses for update to authenticated using (public.has_permission(auth.uid(), account_id, 'courses.write'::public.app_permissions));
create policy courses_delete on public.courses for delete to authenticated using (public.has_permission(auth.uid(), account_id, 'courses.write'::public.app_permissions));
create trigger trg_courses_updated_at before update on public.courses for each row execute function public.update_account_settings_timestamp();

-- Course sessions (individual dates/times)
create table if not exists public.course_sessions (
  id uuid primary key default gen_random_uuid(),
  course_id uuid not null references public.courses(id) on delete cascade,
  session_date date not null,
  start_time time not null,
  end_time time not null,
  location_id uuid references public.course_locations(id) on delete set null,
  notes text,
  is_cancelled boolean not null default false,
  created_at timestamptz not null default now()
);
create index ix_course_sessions_course on public.course_sessions(course_id);
create index ix_course_sessions_date on public.course_sessions(session_date);
alter table public.course_sessions enable row level security;
revoke all on public.course_sessions from authenticated, service_role;
grant select, insert, update, delete on public.course_sessions to authenticated;
grant all on public.course_sessions to service_role;
create policy course_sessions_select on public.course_sessions for select to authenticated using (exists (select 1 from public.courses c where c.id = course_sessions.course_id and public.has_role_on_account(c.account_id)));
create policy course_sessions_mutate on public.course_sessions for all to authenticated using (exists (select 1 from public.courses c where c.id = course_sessions.course_id and public.has_permission(auth.uid(), c.account_id, 'courses.write'::public.app_permissions)));

-- Course participants (enrollments)
create table if not exists public.course_participants (
  id uuid primary key default gen_random_uuid(),
  course_id uuid not null references public.courses(id) on delete cascade,
  member_id uuid references public.members(id) on delete set null,
  first_name text not null,
  last_name text not null,
  email text,
  phone text,
  status public.enrollment_status not null default 'enrolled',
  enrolled_at timestamptz not null default now(),
  cancelled_at timestamptz,
  fee_paid numeric(10,2) default 0,
  notes text,
  unique(course_id, member_id)
);
create index ix_course_participants_course on public.course_participants(course_id);
create index ix_course_participants_member on public.course_participants(member_id);
alter table public.course_participants enable row level security;
revoke all on public.course_participants from authenticated, service_role;
grant select, insert, update, delete on public.course_participants to authenticated;
grant all on public.course_participants to service_role;
create policy course_participants_select on public.course_participants for select to authenticated using (exists (select 1 from public.courses c where c.id = course_participants.course_id and public.has_role_on_account(c.account_id)));
create policy course_participants_mutate on public.course_participants for all to authenticated using (exists (select 1 from public.courses c where c.id = course_participants.course_id and public.has_permission(auth.uid(), c.account_id, 'courses.write'::public.app_permissions)));

-- Attendance
create table if not exists public.course_attendance (
  id uuid primary key default gen_random_uuid(),
  session_id uuid not null references public.course_sessions(id) on delete cascade,
  participant_id uuid not null references public.course_participants(id) on delete cascade,
  present boolean not null default false,
  notes text,
  unique(session_id, participant_id)
);
create index ix_course_attendance_session on public.course_attendance(session_id);
alter table public.course_attendance enable row level security;
revoke all on public.course_attendance from authenticated, service_role;
grant select, insert, update, delete on public.course_attendance to authenticated;
grant all on public.course_attendance to service_role;
create policy course_attendance_select on public.course_attendance for select to authenticated using (exists (select 1 from public.course_sessions s join public.courses c on c.id = s.course_id where s.id = course_attendance.session_id and public.has_role_on_account(c.account_id)));
create policy course_attendance_mutate on public.course_attendance for all to authenticated using (exists (select 1 from public.course_sessions s join public.courses c on c.id = s.course_id where s.id = course_attendance.session_id and public.has_permission(auth.uid(), c.account_id, 'courses.write'::public.app_permissions)));
