import { type ShiftRotation } from 'src/types/shifts';
import { type LokaliseTFunction } from 'src/utils/i18n';

export type RotationDaysSummary = {
  /** The leading number that should render semibold. */
  total: string;
  /** Everything that comes after the bold total (already includes the leading space when non-empty). */
  detail: string;
};

/**
 * Builds the structured "days summary" for a shift rotation, ready to compose
 * inline with the total in bold. Relies on i18next's native pluralization
 * (keys ending in `_one` / `_other`) so grammar reads naturally in Spanish.
 *
 * Always renders as `<total> (<detail>)`:
 *   - Both shifts and rests:  **4** (2 turnos, 2 descansos)
 *   - Only shifts:            **3** (3 turnos)
 *   - Only rests:             **2** (2 descansos)
 */
export const formatRotationDaysSummary = (
  t: LokaliseTFunction,
  rotation: ShiftRotation,
): RotationDaysSummary => {
  const hasShifts = rotation.workDaysCount > 0;
  const hasRests = rotation.restDaysCount > 0;

  if (!hasShifts && !hasRests) {
    return { total: '', detail: '' };
  }

  const parts: string[] = [];
  if (hasShifts) {
    parts.push(t('templates.shifts_count', { count: rotation.workDaysCount }));
  }
  if (hasRests) {
    parts.push(
      t('templates.rest_days_count', { count: rotation.restDaysCount }),
    );
  }

  return {
    total: String(rotation.cycleLength),
    detail: ` (${parts.join(', ')})`,
  };
};
