import { Dependents, Education, Gender, Income, MaritalStatus, Seniority, UserRegisterPayload } from "../models/types";

export const getScore = (user: UserRegisterPayload) => {
  let score = 0;

  // Antiguedad
  const seniorityScore = 20;
  if (user.seniority === Seniority.OneYear) score += seniorityScore * 0.3;
  else if (user.seniority === Seniority.ThreeYears) score += seniorityScore * 0.7;
  else if (user.seniority === Seniority.ThreeYearsPlus) score += seniorityScore;

  // Estado civil
  const maritalStatusScore = 5;
  if (user.maritalStatus === MaritalStatus.Single) score += maritalStatusScore * 0.5;
  else if (user.maritalStatus === MaritalStatus.Married) score += maritalStatusScore;

  // Telefono
  if (user.userAgent) {
    const osVersion = getOSVersion(user.userAgent);
    const phoneScore = 5;
    if (osVersion.includes("Android")) {
      if (isHigherThanAndroid12(user.userAgent)) score += phoneScore;
      else score += phoneScore * 0.5;
    }
  }

  // Capadidad endeudamiento
  const incomeScore = 20;
  if (user.income !== Income.VeryLow && user.income !== Income.Low) score += incomeScore;

  // Genero
  const genderScore = 5;
  if (user.gender === Gender.Female) score += genderScore;

  // Dependientes
  const dependentsScore = 15;
  if (user.dependents === Dependents.Zero) score += dependentsScore;
  else if (user.dependents === Dependents.One) score += dependentsScore * 0.5;

  // Edad
  const age = getAge(user.birthDate);
  const ageScore = 15;
  if (age >= 23 && age <= 45) score += ageScore;
  else if (age > 45 && age <= 60) score += ageScore * 0.7;

  // Educacion
  const educationScore = 15;
  if (user.education === Education.Primary) score += educationScore * 0.2;
  else if (user.education === Education.HighSchool) score += educationScore * 0.7;
  else if (user.education === Education.Bachelor) score += educationScore;

  return score;
};

export function getAge(birthDate: string | Date): number {
  const birth = typeof birthDate === "string" ? new Date(birthDate) : birthDate;
  const today = new Date();

  let age = today.getFullYear() - birth.getFullYear();

  // Comprobar si el cumpleaños de este año ya ha ocurrido
  const monthDifference = today.getMonth() - birth.getMonth();
  const dayDifference = today.getDate() - birth.getDate();

  if (monthDifference < 0 || (monthDifference === 0 && dayDifference < 0)) {
    age--;
  }

  return age;
}

function getOSVersion(userAgent: string) {
  const ua = userAgent || "";
  let versionOS = "Desconocida";

  // Ejemplo básico para Android:
  if (ua.includes("Android")) {
    const regex = /Android\s([0-9\.]+)/;
    const match = ua.match(regex);
    if (match && match[1]) {
      versionOS = `Android ${match[1]}`;
    }
  }

  // Ejemplo básico para iOS:
  if (/iPhone|iPad|iPod/.test(ua)) {
    const regex = /OS\s([0-9_]+)/;
    const match = ua.match(regex);
    if (match && match[1]) {
      // Reemplazamos guiones bajos por puntos
      versionOS = `iOS ${match[1].replace(/_/g, ".")}`;
    }
  }

  return versionOS;
}

function isHigherThanAndroid12(userAgent: string) {
  const regex = /Android\s([0-9\.]+)/i;
  const match = userAgent.match(regex);

  if (match && match[1]) {
    // Convertimos la versión a número (se considera la parte entera)
    const version = parseFloat(match[1]);
    return version > 12;
  }
}
