import dayjs from 'dayjs';
import type { SlaRule, SlaSettings, SlaWorkingHoursPayload } from '../interface';

// The rules, with the feature-control row each one is switched on from.
export const SLA_RULES: Record<SlaRule, { label: string, short: string, slug: string }> = {
  sla: { label: '30-Minute SLA', short: '30-minute SLA', slug: 'auto-lead-sla-reassignment' },
  overdue: { label: 'Overdue SLA', short: 'overdue SLA', slug: 'auto-lead-overdue-reassignment' },
  lead_request: { label: 'Lead Request Transfer', short: 'lead request transfer', slug: 'lead-request-daily-transfer' },
};

// The daily lead request transfer runs at one fixed time, so it has no working hours, only closed days.
export const LEAD_REQUEST_RUN_TIME = '10:30 AM';
export const hasWorkingHours = (rule: SlaRule): boolean => rule !== 'lead_request';

// The SLA a feature-control row switches, or null for any other row.
export const ruleForSlug = (slug: string): SlaRule | null => (
  (Object.keys(SLA_RULES) as SlaRule[]).find((rule) => SLA_RULES[rule].slug === slug) ?? null
);

// The 30-minute and the overdue SLA are a pair that can be marked together; the lead request transfer stands alone.
export const otherRule = (rule: SlaRule): SlaRule | null => (rule === 'sla' ? 'overdue' : rule === 'overdue' ? 'sla' : null);

// Index = the weekday number the backend uses (0 = Sunday ... 6 = Saturday).
export const WEEKDAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

// When one SLA works: opening and closing time ('HH:MM') and the weekdays it is closed all day.
export interface Schedule {
  start: string
  end: string
  offDays: number[]
}

export const scheduleOf = (settings: SlaSettings, rule: SlaRule): Schedule => {
  if (rule === 'sla') return { start: settings.work_start, end: settings.work_end, offDays: settings.off_days };
  if (rule === 'overdue') return { start: settings.overdue_work_start, end: settings.overdue_work_end, offDays: settings.overdue_off_days };
  // No working hours: the times are only placeholders and are never shown or saved for this rule.
  return { start: '10:30', end: '10:30', offDays: settings.lead_request_off_days ?? [] };
};

// The save request for one SLA's schedule; the other SLA's fields are left out, so they stay as they are.
export const schedulePayload = (rule: SlaRule, schedule: Schedule): SlaWorkingHoursPayload => {
  if (rule === 'sla') return { work_start: schedule.start, work_end: schedule.end, off_days: schedule.offDays };
  if (rule === 'overdue') return { overdue_work_start: schedule.start, overdue_work_end: schedule.end, overdue_off_days: schedule.offDays };
  return { lead_request_off_days: schedule.offDays };
};

// '20:00' -> '8:00 PM'
export const formatClock = (time: string): string => dayjs(`2000-01-01T${time}`).format('h:mm A');

// Two names are joined with and; three or more as A, B and C.
export const joinNames = (names: string[]): string => (
  names.length > 1 ? `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}` : names[0] ?? ''
);

export const closedDayNames = (offDays: number[]): string[] => [...offDays].sort((a, b) => a - b).map((day) => WEEKDAY_NAMES[day]);

// '10:00 AM – 8:00 PM'
export const describeHours = (schedule: Schedule): string => `${formatClock(schedule.start)} – ${formatClock(schedule.end)}`;

// For example: 10:00 AM – 8:00 PM, closed on Monday - or, with no closed day, 10:00 AM – 8:00 PM, every day.
export const describeSchedule = (schedule: Schedule): string => {
  const closed = closedDayNames(schedule.offDays);
  return closed.length > 0 ? `${describeHours(schedule)}, closed on ${joinNames(closed)}` : `${describeHours(schedule)}, every day`;
};
