// Work Availability (register + edit) input rules.
//   Chat / Call rate  : a number, 0 or more, at most 2 decimals.
//   Daily working hrs : a whole number from 1 to 24 (no decimals, no negatives).

export interface AvailabilityErrors {
  chatRate?: string;
  callRate?: string;
  dailyHours?: string;
}

export function validateAvailability(values: {
  chatRate: string;
  callRate: string;
  dailyHours: string;
}): AvailabilityErrors {
  const errors: AvailabilityErrors = {};

  const rate = (raw: string, label: string): string | undefined => {
    const text = raw.trim();

    if (text === "") return `Enter the ${label}.`;
    if (!/^\d+(\.\d{1,2})?$/.test(text)) {
      return `${label[0].toUpperCase()}${label.slice(1)} must be a number of 0 or more (up to 2 decimals).`;
    }
    return undefined;
  };

  errors.chatRate = rate(values.chatRate, "chat rate");
  errors.callRate = rate(values.callRate, "call rate");

  const hours = values.dailyHours.trim();

  if (hours === "") {
    errors.dailyHours = "Enter your daily working hours.";
  } else if (!/^\d+$/.test(hours)) {
    errors.dailyHours = "Daily working hours must be a whole number (no decimals or negatives).";
  } else if (Number(hours) < 1 || Number(hours) > 24) {
    errors.dailyHours = "Daily working hours must be between 1 and 24.";
  }

  return errors;
}

export function hasAvailabilityErrors(errors: AvailabilityErrors): boolean {
  return Object.values(errors).some(Boolean);
}

// Keys that make a number input accept a negative sign / exponent (and, for
// whole-number fields, a decimal point).
export function blockInvalidNumberKeys(wholeNumber: boolean) {
  const blocked = wholeNumber ? ["-", "+", "e", "E", ".", ","] : ["-", "+", "e", "E"];

  return (event: React.KeyboardEvent<HTMLInputElement>) => {
    if (blocked.includes(event.key)) event.preventDefault();
  };
}

// Basic Details step (register + edit): whole numbers only — the backend's
// `experience_years` / `daily_hours` are ints (a decimal such as 8.5 is
// rejected with "cannot unmarshal number 8.5 into … of type int").
export interface BasicNumberErrors {
  experience?: string;
  dailyHours?: string;
}

export function validateBasicNumbers(values: {
  experience: string;
  dailyHours: string;
}): BasicNumberErrors {
  const errors: BasicNumberErrors = {};
  const exp = values.experience.trim();
  const hours = values.dailyHours.trim();

  if (exp !== "") {
    if (!/^\d+$/.test(exp)) errors.experience = "Experience must be a whole number (no decimals or negatives).";
    else if (Number(exp) > 70) errors.experience = "Experience must be 70 years or less.";
  }

  if (hours !== "") {
    if (!/^\d+$/.test(hours)) errors.dailyHours = "Daily hours must be a whole number (no decimals or negatives).";
    else if (Number(hours) < 1 || Number(hours) > 24) errors.dailyHours = "Daily hours must be between 1 and 24.";
  }

  return errors;
}

// A saved "8.5" / "10.0" must show as a whole number.
export function toWholeNumberText(value: unknown): string | undefined {
  const n = Number(value);

  return value !== undefined && value !== null && value !== "" && Number.isFinite(n) && n >= 0
    ? String(Math.trunc(n))
    : undefined;
}

// "Experience years" on the Experiences / Professional steps: a whole number
// from 0 to 70 (empty is left to each step's own required-field rule).
export function experienceYearsError(value: string): string | undefined {
  const text = value.trim();

  if (text === "") return undefined;
  if (!/^\d+$/.test(text)) return "Experience years must be a whole number (no decimals or negatives).";
  if (Number(text) > 70) return "Experience years must be 70 or less.";

  return undefined;
}

// "Years of experience with these skills" (Skills step): whole number 0–70.
export function skillExperienceError(value: string): string | undefined {
  const text = value.trim();

  if (text === "") return undefined;
  if (!/^\d+$/.test(text)) return "Years of experience must be a whole number (no decimals or negatives).";
  if (Number(text) > 70) return "Years of experience must be 70 or less.";

  return undefined;
}
