const MODULE_ID = 'mgt2-compendium-amiral-denisov';

export function imperialToSolomani(impYear) {
  return Math.round(impYear * 1.0006644 + 4521);
}

export function imperialToVilani(impYear) {
  return Math.round((impYear + 4045) / 1.312);
}

export function imperialToZhodani(impYear) {
  const raw = (impYear + 6731) / 2.26;
  const olympiad = Math.floor(raw);
  const frac = raw - olympiad;
  const year = Math.min(3, Math.max(1, Math.round(frac * 3)));
  return { olympiad, year, label: `${olympiad}.${year}` };
}

export function imperialToAslan(impYear) {
  return Math.round((impYear + 2083) / 0.877);
}

export function imperialToKkree(impYear) {
  return Math.round((impYear + 8145) / 1.2);
}

export function imperialToHiver(impYear) {
  return Math.round((impYear - 410) * 2);
}

export function computeConversions(impYear) {
  const y = impYear ?? 1116;
  const z = imperialToZhodani(y);
  return {
    solomani: `${imperialToSolomani(y)} AD`,
    vilani: `${imperialToVilani(y)} VI`,
    zhodani: z.label,
    aslan: `${imperialToAslan(y)}`,
    kkree: `${imperialToKkree(y)}`,
    hiver: `${imperialToHiver(y)} HF`,
  };
}

export const DAY_NAMES = [
  'Holiday',
  'Wonday',
  'Tuday',
  'Thirday',
  'Forday',
  'Fiday',
  'Sixday',
  'Senday',
];

export const DAY_LABELS_FR = {
  Holiday: 'Holiday',
  Wonday: 'Wonday',
  Tuday: 'Tuday',
  Thirday: 'Thirday',
  Forday: 'Forday',
  Fiday: 'Fiday',
  Sixday: 'Sixday',
  Senday: 'Senday',
};

export function getDayLabel(day) {
  if (day < 1 || day > 365) return '?';
  if (day === 1) return DAY_NAMES[0];
  return DAY_NAMES[((day - 2) % 7) + 1];
}

export function getWeekday(day) {
  if (day < 1 || day > 365) return -1;
  if (day === 1) return -1;
  return (day - 2) % 7;
}

export function formatImperialDate({ year, day, hour, minute, planet } = {}) {
  const y = year ?? 1116;
  const d = day ?? 1;
  const dayLabel = getDayLabel(d);
  const dayStr = String(d).padStart(3, '0');
  let label = `${dayLabel}, ${dayStr}-${y} IC`;
  if (hour != null && minute != null) {
    const h = String(Math.round(hour)).padStart(2, '0');
    const m = String(Math.round(minute)).padStart(2, '0');
    label += ` ─ ${h}:${m}`;
    if (planet) label += ` (${planet})`;
  }
  return label;
}

export function getDefaultCalendar() {
  return { year: 1116, day: 1, hour: 12, minute: 0, planet: '' };
}

export function formatRealDate() {
  const d = new Date();
  const y = d.getFullYear();
  const m = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const h = String(d.getHours()).padStart(2, '0');
  const min = String(d.getMinutes()).padStart(2, '0');
  return `${day}/${m}/${y} ${h}:${min}`;
}
