export type TaxCategory = "nomina" | "admin";

export type Country = {
  id: string;
  code: string;
  name: string;
  isRegional: boolean;
  sortOrder: number;
};

export type TaxEvent = {
  id: string;
  date: string;
  text: string;
  countryId: string;
  country: string;
  countryCode: string;
  category: TaxCategory;
  source?: string;
  hasSentNotification: boolean;
  firstSentAt: string | null;
  sentNotificationCount: number;
};

export type ContactArea = "admin" | "nomina" | "regional" | "other";

export type Contact = {
  id: string;
  countryId: string;
  country: string;
  countryCode: string;
  name: string;
  email: string;
  role: string;
  area: ContactArea;
  department: string;
  division: string;
  location: string;
  source: "manual" | "bamboohr" | string;
  bambooEmployeeId: string;
  bambooEmployeeNumber: string;
  bambooStatus: string;
  emailEnabled: boolean;
  googleChatEnabled: boolean;
  calendarEnabled: boolean;
};

export type BambooContactCandidate = {
  id: string;
  employeeNumber: string;
  fullName: string;
  displayName: string;
  email: string;
  jobTitle: string;
  department: string;
  division: string;
  location: string;
  countryCode: string;
  countryName: string;
  area: ContactArea;
  status: string;
  score: number;
  matchQuality: "exacta" | "alta" | "media";
};

// Siempre se alerta el miércoles anterior (estrictamente anterior) y la fecha
// límite. Esto incluye obligaciones que vencen sábados y domingos.
export function getAlertDates(dueISO: string): string[] {
  const [year, month, day] = dueISO.split("-").map(Number);
  const due = new Date(Date.UTC(year, month - 1, day));
  const dow = due.getUTCDay(); // 0=domingo ... 6=sábado
  const daysSinceWednesday = (dow - 3 + 7) % 7;
  const strictPreviousWednesday = daysSinceWednesday === 0 ? 7 : daysSinceWednesday;

  const previousWednesday = new Date(due);
  previousWednesday.setUTCDate(due.getUTCDate() - strictPreviousWednesday);

  return [toISO(previousWednesday), dueISO].filter(
    (value, index, all) => all.indexOf(value) === index,
  );
}

function toISO(date: Date): string {
  return `${date.getUTCFullYear()}-${String(date.getUTCMonth() + 1).padStart(2, "0")}-${String(date.getUTCDate()).padStart(2, "0")}`;
}

export const MONTHS_ES = [
  "Enero",
  "Febrero",
  "Marzo",
  "Abril",
  "Mayo",
  "Junio",
  "Julio",
  "Agosto",
  "Septiembre",
  "Octubre",
  "Noviembre",
  "Diciembre",
];

export const DAYS_ES = ["Lun", "Mar", "Mié", "Jue", "Vie", "Sáb", "Dom"];

export function formatDate(iso: string): string {
  const [year, month, day] = iso.split("-").map(Number);
  return `${day} ${MONTHS_ES[month - 1]} ${year}`;
}

export function normalizeSearch(value: string): string {
  return value
    .normalize("NFD")
    .replace(/[\u0300-\u036f]/g, "")
    .toLowerCase()
    .trim();
}
