-- ============================================================
-- Migration: fn_get_session_attendance_report_field_definitions
--
-- Field registry for the session-wise attendance report (vw_program_session_attendance_report /
-- fn_generate_session_attendance_report). Same ReportFieldDef shape as
-- fn_get_report_field_definitions, kept as a separate function since this is
-- a distinct report/field domain (no program-flag gating needed — this
-- report only applies to multi-session programs to begin with).
-- ============================================================

DROP FUNCTION IF EXISTS fn_get_session_attendance_report_field_definitions;

CREATE OR REPLACE FUNCTION fn_get_session_attendance_report_field_definitions()
RETURNS TABLE(
  key                TEXT,
  alias              TEXT,
  label              TEXT,
  "group"            TEXT,
  "order"            INT,
  format             TEXT,
  "dependsOn"        TEXT,
  "requiredFlags"    TEXT[],
  "conditionalFormat" JSONB
)
STABLE LANGUAGE sql AS $$
  SELECT
    t.key, t.alias, t.label, t.grp, t.ord, t.fmt, t.dep, t.flags, t.cond_fmt
  FROM (VALUES

    -- ── Session ───────────────────────────────────────────────────────────────
    ('sessionName',            'sessionName',            'Session Name',            'Session',  1, NULL,       NULL::TEXT, NULL::TEXT[], NULL::JSONB),
    ('sessionStartsAt',        'sessionStartsAt',        'Session Date',            'Session',  2, 'datetime', NULL,       NULL,         NULL),

    -- ── Attendee ──────────────────────────────────────────────────────────────
    ('registrationSeqNumber',  'registrationSeqNumber',  'Registration Seq No.',    'Attendee', 3, NULL,       NULL,       NULL,         NULL),
    ('fullName',               'fullName',               'Full Name',               'Attendee', 4, NULL,       NULL,       NULL,         NULL),
    ('email',                  'email',                  'Email',                   'Attendee', 5, NULL,       NULL,       NULL,         NULL),
    ('phone',                  'phone',                  'Mobile',                  'Attendee', 6, NULL,       NULL,       NULL,         NULL),
    ('activationStatus',       'activationStatus',       'Activation Status',       'Attendee', 7, 'title-case', NULL,     NULL,         NULL),

    -- ── Attendance ────────────────────────────────────────────────────────────
    -- attendanceStatus is the precedence-resolved final call (program_user_attendance.is_attended);
    -- systemAttendance/rmAttendance/coordinatorAttendance break it down by who/what marked it.
    ('attendanceStatus',       'attendanceStatus',       'Final Attendance',        'Attendance', 8,  NULL,     NULL, NULL, NULL),
    ('systemAttendance',       'systemAttendance',       'System Attendance',       'Attendance', 9,  'boolean',NULL, NULL, NULL),
    ('rmAttendance',           'rmAttendance',           'RM Attendance',           'Attendance', 10, 'boolean',NULL, NULL, NULL),
    ('coordinatorAttendance',  'coordinatorAttendance',  'Coordinator Attendance',  'Attendance', 11, 'boolean',NULL, NULL, NULL),
    ('joinedTime',             'joinedTime',             'Joined Time',             'Attendance', 12, 'datetime',NULL, NULL, NULL),
    ('noOfLogins',             'noOfLogins',             'No. of Logins',           'Attendance', 13, 'number', NULL, NULL, NULL)

  ) AS t(key, alias, label, grp, ord, fmt, dep, flags, cond_fmt)
  ORDER BY t.ord
$$;
