import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

export function formatNumber(n: number): string {
  if (n >= 1_000_000) return (n / 1_000_000).toFixed(1) + "M";
  if (n >= 1_000) return (n / 1_000).toFixed(1) + "K";
  return n.toString();
}

export function formatDuration(seconds: number): string {
  const h = Math.floor(seconds / 3600);
  const m = Math.floor((seconds % 3600) / 60);
  const s = seconds % 60;
  if (h > 0) return `${h}:${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
  return `${m}:${String(s).padStart(2, "0")}`;
}

export function formatDate(date: Date | string): string {
  return new Date(date).toLocaleDateString("fr-FR", {
    day: "2-digit",
    month: "short",
    year: "numeric",
  });
}

export function timeAgo(date: Date | string): string {
  const seconds = Math.floor((Date.now() - new Date(date).getTime()) / 1000);
  if (seconds < 60) return "à l'instant";
  if (seconds < 3600) return `il y a ${Math.floor(seconds / 60)} min`;
  if (seconds < 86400) return `il y a ${Math.floor(seconds / 3600)}h`;
  if (seconds < 604800) return `il y a ${Math.floor(seconds / 86400)}j`;
  return formatDate(date);
}

export function translateTitleFr(title: string, titleFr?: string | null): string {
  if (titleFr && titleFr.trim().length > 0) return titleFr;
  if (!title) return "";

  let result = title;

  const replacements: [RegExp, string][] = [
    [/\bstep\s*sister\b/gi, "demi-sœur"],
    [/\bstep\s*sisters\b/gi, "demi-sœurs"],
    [/\bstep\s*mom\b/gi, "belle-mère"],
    [/\bstep\s*mother\b/gi, "belle-mère"],
    [/\bstep\s*brother\b/gi, "demi-frère"],
    [/\bstep\s*dad\b/gi, "beau-père"],
    [/\bstep\s*father\b/gi, "beau-père"],
    [/\bstep\s*daughter\b/gi, "belle-fille"],
    [/\bbig\s*tits\b/gi, "gros seins"],
    [/\bbig\s*boobs\b/gi, "grosse poitrine"],
    [/\bbig\s*ass\b/gi, "gros cul"],
    [/\bbig\s*butt\b/gi, "gros fessier"],
    [/\bbig\s*dick\b/gi, "grosse bite"],
    [/\bbig\s*cock\b/gi, "grosse bite"],
    [/\bcreampie\b/gi, "éjaculation interne"],
    [/\bcumshot\b/gi, "éjaculation"],
    [/\bblowjob\b/gi, "fellation"],
    [/\bhandjob\b/gi, "masturbation"],
    [/\bdeepthroat\b/gi, "gorge profonde"],
    [/\bthreesome\b/gi, "plan à trois"],
    [/\bfoursome\b/gi, "plan à quatre"],
    [/\bgangbang\b/gi, "gangbang"],
    [/\blesbian\b/gi, "lesbienne"],
    [/\blesbians\b/gi, "lesbiennes"],
    [/\bhot\b/gi, "sexy"],
    [/\bhorny\b/gi, "excitée"],
    [/\bnaughty\b/gi, "coquine"],
    [/\bslut\b/gi, "salope"],
    [/\bslutty\b/gi, "coquine"],
    [/\bbitch\b/gi, "salope"],
    [/\bteen\b/gi, "ado"],
    [/\bteens\b/gi, "ados"],
    [/\byoung\b/gi, "jeune"],
    [/\bblonde\b/gi, "blonde"],
    [/\bbrunette\b/gi, "brune"],
    [/\bredhead\b/gi, "rousse"],
    [/\bbabe\b/gi, "canon"],
    [/\bbabes\b/gi, "canons"],
    [/\bteacher\b/gi, "professeure"],
    [/\bstudent\b/gi, "étudiante"],
    [/\bnurse\b/gi, "infirmière"],
    [/\bmaid\b/gi, "femme de chambre"],
    [/\boffice\b/gi, "bureau"],
    [/\bshower\b/gi, "douche"],
    [/\bpool\b/gi, "piscine"],
    [/\boutdoor\b/gi, "plein air"],
    [/\bpublic\b/gi, "public"],
    [/\bfirst\s*time\b/gi, "première fois"],
    [/\bwild\b/gi, "sauvage"],
    [/\bhardcore\b/gi, "hardcore"],
    [/\bwet\b/gi, "mouillée"],
    [/\bpussy\b/gi, "chatte"],
    [/\bboobs\b/gi, "seins"],
    [/\bbutt\b/gi, "fesses"],
    [/\bboyfriend\b/gi, "copain"],
    [/\bgirlfriend\b/gi, "copine"],
    [/\bwife\b/gi, "femme"],
    [/\bhusband\b/gi, "mari"],
    [/\bneighbor\b/gi, "voisine"],
    [/\bcheating\b/gi, "infidèle"],
    [/\bcaught\b/gi, "surprise"],
    [/\bsecret\b/gi, "secret"],
    [/\bhidden\s*cam\b/gi, "caméra cachée"],
  ];

  for (const [pattern, replacement] of replacements) {
    result = result.replace(pattern, replacement);
  }

  if (result.length > 0) {
    result = result.charAt(0).toUpperCase() + result.slice(1);
  }

  return result;
}

export interface ModelProfile {
  name: string;
  avatar: string;
}

const MODEL_PROFILES: ModelProfile[] = [
  { name: "Eva Elfie", avatar: "https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=150&q=80" },
  { name: "Lana Rhoades", avatar: "https://images.unsplash.com/photo-1517841905240-472988babdf9?w=150&q=80" },
  { name: "Abella Danger", avatar: "https://images.unsplash.com/photo-1524504388940-b1c1722653e1?w=150&q=80" },
  { name: "Angela White", avatar: "https://images.unsplash.com/photo-1494790108377-be9c29b29330?w=150&q=80" },
  { name: "Riley Reid", avatar: "https://images.unsplash.com/photo-1529626455594-4ff0802cfb7e?w=150&q=80" },
  { name: "Sweetie Fox", avatar: "https://images.unsplash.com/photo-1488426862026-3ee34a7d66df?w=150&q=80" },
  { name: "Chloé", avatar: "https://images.unsplash.com/photo-1544005313-94ddf0286df2?w=150&q=80" },
  { name: "Léa", avatar: "https://images.unsplash.com/photo-1508214751196-bcfd4ca60f91?w=150&q=80" },
  { name: "Clara", avatar: "https://images.unsplash.com/photo-1531746020798-e6953c6e8e04?w=150&q=80" },
  { name: "Océane", avatar: "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=150&q=80" },
  { name: "Mélanie", avatar: "https://images.unsplash.com/photo-1524250502761-1ac6f2e30d43?w=150&q=80" },
  { name: "Inès", avatar: "https://images.unsplash.com/photo-1519085360753-af0119f7cbe7?w=150&q=80" },
  { name: "Camille", avatar: "https://images.unsplash.com/photo-1567532939604-b6b5b0db2604?w=150&q=80" },
  { name: "Sophie", avatar: "https://images.unsplash.com/photo-1573496359142-b8d87734a5a2?w=150&q=80" },
  { name: "Amandine", avatar: "https://images.unsplash.com/photo-1539571696357-5a69c17a67c6?w=150&q=80" },
  { name: "Sasha Grey", avatar: "https://images.unsplash.com/photo-1501196354995-cbb51c65aaea?w=150&q=80" },
  { name: "Mia Khalifa", avatar: "https://images.unsplash.com/photo-1520813792240-56fc4a3765a7?w=150&q=80" },
  { name: "Kendra", avatar: "https://images.unsplash.com/photo-1517841905240-472988babdf9?w=150&q=80" },
  { name: "Vicky", avatar: "https://images.unsplash.com/photo-1524504388940-b1c1722653e1?w=150&q=80" },
  { name: "Célia", avatar: "https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=150&q=80" }
];

export function resolveModelProfile(title: string, uploader?: { name?: string | null; image?: string | null } | null, id?: string): ModelProfile {
  // 1. If uploader has a real name & image, use it!
  if (uploader?.name && uploader.name !== "Anonyme" && uploader.name !== "100% française") {
    return {
      name: uploader.name,
      avatar: uploader.image || "https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=150&q=80"
    };
  }

  // 2. Only match an adult star's name if it is ACTUALLY in the title
  const text = (title || "").toLowerCase();
  for (const profile of MODEL_PROFILES) {
    if (text.includes(profile.name.toLowerCase())) {
      return profile;
    }
  }

  // 3. Fallback: Clean neutral channel badge without forcing wrong star names
  const seed = (id || title || "model").split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
  const fallbackAvatar = MODEL_PROFILES[seed % MODEL_PROFILES.length].avatar;

  return {
    name: "Modèle Officiel",
    avatar: uploader?.image || fallbackAvatar
  };
}
