-- =====================================================
-- Additional Indexes
--
-- Partial indexes for common query patterns across
-- course-management, event-management, and
-- booking-management modules.
-- =====================================================

-- Course participants: fast capacity counting
CREATE INDEX IF NOT EXISTS ix_course_participants_active_status
  ON public.course_participants(course_id, status)
  WHERE status IN ('enrolled', 'waitlisted');

-- Event registrations: fast registration counting
CREATE INDEX IF NOT EXISTS ix_event_registrations_active_status
  ON public.event_registrations(event_id, status)
  WHERE status IN ('confirmed', 'pending', 'waitlisted');

-- Bookings: active bookings for availability queries
CREATE INDEX IF NOT EXISTS ix_bookings_active_dates
  ON public.bookings(room_id, check_in, check_out)
  WHERE status NOT IN ('cancelled', 'no_show');

-- Bookings: guest history lookup
CREATE INDEX IF NOT EXISTS ix_bookings_guest_checkin
  ON public.bookings(guest_id, check_in DESC)
  WHERE guest_id IS NOT NULL;

-- Course sessions: instructor scheduling conflict checks
CREATE INDEX IF NOT EXISTS ix_course_sessions_instructor_date
  ON public.course_sessions(session_date, start_time, end_time)
  WHERE is_cancelled = false;

-- Audit log indexes for timeline queries
-- Safety nets in case earlier migration did not cover them
CREATE INDEX IF NOT EXISTS ix_course_audit_account_action
  ON public.course_audit_log(account_id, action);

CREATE INDEX IF NOT EXISTS ix_event_audit_account_action
  ON public.event_audit_log(account_id, action);

CREATE INDEX IF NOT EXISTS ix_booking_audit_account_action
  ON public.booking_audit_log(account_id, action);
