// Shared types + display helpers for the My Schedule Master exports.
// Pure presentation — no DB, no Nest. Lives with the templates so the
// audit-schedules service stays free of report-formatting concerns.

export interface MasterCounts {
  initial: number;
  surveillance: number;
  recertification: number;
  total: number;
}

// One flattened, display-ready row used by both Excel and PDF.
export interface MasterRecord {
  sno: number;
  audit_code: string;
  audit_type: string;
  company: string;
  standards: string;
  accreditation: string;
  stage: string;
  mode: string;
  coordinator: string;
  group: string;
  date: string;
  time: string;
  lead_auditor: string;
  status: string;
}

export function prettyType(t?: string | null): string {
  const map: Record<string, string> = {
    INITIAL: 'Initial',
    SURVEILLANCE: 'Surveillance',
    RECERTIFICATION: 'Re-Certification',
  };
  return map[String(t || '').toUpperCase()] || (t ? String(t) : '—');
}

export function prettyMode(m?: string | null): string {
  const map: Record<string, string> = {
    ONSITE: 'Onsite',
    OFFICE: 'Office',
    REMOTE: 'Remote',
    HYBRID: 'Hybrid',
    ONLINE: 'Online',
  };
  return map[String(m || '').toUpperCase()] || (m ? String(m) : '—');
}

export function prettyStatus(s?: string | null): string {
  const map: Record<string, string> = {
    PENDING: 'Pending',
    CONFIRMED: 'Confirmed',
    IN_PROGRESS: 'In Progress',
    COMPLETED: 'Completed',
    CANCELLED: 'Cancelled',
    RESCHEDULED: 'Rescheduled',
  };
  return map[String(s || '').toUpperCase()] || (s ? String(s) : '—');
}

export function fullName(u: any): string {
  if (!u) return '—';
  const f = u.firstName || '';
  const l = u.lastName || '';
  const both = `${f} ${l}`.trim();
  return both || u.email || '—';
}

// Coordinator for INITIAL audits; submitter (audit-request requested_by)
// for Surveillance / Re-Certification — same rule as the detail modal.
// Falls back to coordinator if no submitter is attached.
export function contactNameForRow(row: any): string {
  const type = String(row.audit_type || '').toUpperCase();
  if (type === 'INITIAL') {
    return row.schedule?.coordinator ? fullName(row.schedule.coordinator) : '—';
  }
  const submitter = row.submitted_by;
  if (submitter) return fullName(submitter);
  // fallback when the row has no linked request submitter
  return row.schedule?.coordinator ? fullName(row.schedule.coordinator) : '—';
}

// Flatten a raw AuditScheduleRow (with relations) into a display record.
export function toMasterRecord(row: any, index: number): MasterRecord {
  return {
    sno: index + 1,
    audit_code: row.audit_code || '—',
    audit_type: prettyType(row.audit_type),
    company: row.company?.name ?? '—',
    standards:
      (row.standards ?? []).map((s: any) => s.name).join(', ') || '—',
    accreditation: row.accreditation ?? '—',
    stage: row.audit_stage ?? '—',
    mode: prettyMode(row.audit_mode),
    coordinator: contactNameForRow(row),
    group: row.schedule?.client_group ?? '—',
    date: row.schedule?.schedule_date ?? '—',
    time: row.audit_time_label ?? row.audit_time ?? '—',
    lead_auditor: row.lead_auditor ? fullName(row.lead_auditor) : '—',
    status: prettyStatus(row.status),
  };
}
