"use client";

import Swal from "sweetalert2";

import { defaultLocale, hasLocale, type Locale } from "@/lib/locales";

type AlertOptions = {
  autoClose?: boolean;
  locale?: Locale;
  timer?: number;
  title?: string;
};

const alertCopy = {
  en: {
    confirm: "OK",
    success: "Success",
    error: "Please check",
    warning: "Heads up",
  },
  hi: {
    confirm: "ठीक है",
    success: "सफल",
    error: "कृपया जांचें",
    warning: "ध्यान दें",
  },
  gu: {
    confirm: "બરાબર",
    success: "સફળ",
    error: "કૃપા કરીને તપાસો",
    warning: "ધ્યાન આપો",
  },
} satisfies Record<Locale, Record<string, string>>;

const messageTranslations: Record<string, Partial<Record<Locale, string>>> = {
  "Something went wrong. Please try again.": {
    hi: "कुछ गलत हो गया। कृपया फिर से कोशिश करें।",
    gu: "કંઈક ખોટું થયું. કૃપા કરીને ફરી પ્રયાસ કરો.",
  },
  "Something went wrong on our server. Please try again later or contact support.": {
    hi: "हमारे सर्वर पर कुछ गलत हो गया। कृपया बाद में फिर से कोशिश करें या सपोर्ट से संपर्क करें।",
    gu: "અમારા સર્વર પર કંઈક ખોટું થયું. કૃપા કરીને પછીથી ફરી પ્રયાસ કરો અથવા સપોર્ટનો સંપર્ક કરો.",
  },
  "Unable to connect to the server. Please check your internet connection or try again later.": {
    hi: "सर्वर से कनेक्ट नहीं हो पा रहा है। कृपया अपना इंटरनेट कनेक्शन जांचें या बाद में फिर से कोशिश करें।",
    gu: "સર્વર સાથે કનેક્ટ થઈ શકતું નથી. કૃપા કરીને તમારું ઇન્ટરનેટ કનેક્શન તપાસો અથવા પછીથી ફરી પ્રયાસ કરો.",
  },
  "Request timeout. Please try again.": {
    hi: "रिक्वेस्ट टाइमआउट हो गई। कृपया फिर से कोशिश करें।",
    gu: "રિક્વેસ્ટ ટાઈમઆઉટ થઈ. કૃપા કરીને ફરી પ્રયાસ કરો.",
  },
  "Please check the highlighted fields.": {
    hi: "कृपया हाइलाइट किए गए फ़ील्ड जांचें।",
    gu: "કૃપા કરીને હાઇલાઇટ કરેલા ફીલ્ડ્સ તપાસો.",
  },
  "Enter a valid 10-digit mobile number": {
    hi: "मान्य 10 अंकों का मोबाइल नंबर दर्ज करें",
    gu: "માન્ય 10 અંકનો મોબાઇલ નંબર દાખલ કરો",
  },
  "Please enter your password.": {
    hi: "कृपया अपना पासवर्ड दर्ज करें।",
    gu: "કૃપા કરીને તમારો પાસવર્ડ દાખલ કરો.",
  },
  "Enter the 6-digit OTP.": {
    hi: "6 अंकों का OTP दर्ज करें।",
    gu: "6 અંકનો OTP દાખલ કરો.",
  },
  "Username must contain at least 3 characters.": {
    hi: "यूज़रनेम में कम से कम 3 अक्षर होने चाहिए।",
    gu: "યુઝરનેમમાં ઓછામાં ઓછા 3 અક્ષર હોવા જોઈએ.",
  },
  "Please enter your full name.": {
    hi: "कृपया अपना पूरा नाम दर्ज करें।",
    gu: "કૃપા કરીને તમારું પૂરું નામ દાખલ કરો.",
  },
  "Please enter a valid email address.": {
    hi: "कृपया मान्य ईमेल पता दर्ज करें।",
    gu: "કૃપા કરીને માન્ય ઇમેઇલ સરનામું દાખલ કરો.",
  },
  "Password must contain at least 8 characters.": {
    hi: "पासवर्ड में कम से कम 8 अक्षर होने चाहिए।",
    gu: "પાસવર્ડમાં ઓછામાં ઓછા 8 અક્ષર હોવા જોઈએ.",
  },
  "Please confirm your password.": {
    hi: "कृपया अपने पासवर्ड की पुष्टि करें।",
    gu: "કૃપા કરીને તમારા પાસવર્ડની ખાતરી કરો.",
  },
  "Please enter your company name.": {
    hi: "कृपया अपनी कंपनी नाम दर्ज करें।",
    gu: "કૃપા કરીને તમારી કંપની નું નામ દાખલ કરો.",
  },
  "Please enter a valid GST number.": {
    hi: "कृपया मान्य GST नंबर दर्ज करें।",
    gu: "કૃપા કરીને માન્ય GST નંબર દાખલ કરો.",
  },
  "Please enter your company address.": {
    hi: "कृपया अपनी कंपनी का पता दर्ज करें।",
    gu: "કૃપા કરીને તમારી કંપનીનું સરનામું દાખલ કરો.",
  },
  "Please enter your city.": {
    hi: "कृपया अपना शहर दर्ज करें।",
    gu: "કૃપા કરીને તમારું શહેર દાખલ કરો.",
  },
  "Please enter your state.": {
    hi: "कृपया अपना राज्य दर्ज करें।",
    gu: "કૃપા કરીને તમારું રાજ્ય દાખલ કરો.",
  },
  "The passwords do not match.": {
    hi: "दोनों पासवर्ड समान नहीं हैं।",
    gu: "બન્ને પાસવર્ડ સરખા નથી.",
  },
  "Please verify your mobile number first.": {
    hi: "कृपया पहले अपना मोबाइल नंबर वेरिफाई करें।",
    gu: "કૃપા કરીને પહેલા તમારો મોબાઇલ નંબર વેરિફાઇ કરો.",
  },
  "OTP verification failed.": {
    hi: "OTP वेरिफिकेशन विफल रहा।",
    gu: "OTP વેરિફિકેશન નિષ્ફળ ગયું.",
  },
  "Please select a valid demo date.": {
    hi: "कृपया मान्य डेमो तारीख चुनें।",
    gu: "કૃપા કરીને માન્ય ડેમો તારીખ પસંદ કરો.",
  },
  "Please choose today or a future date.": {
    hi: "कृपया आज या भविष्य की तारीख चुनें।",
    gu: "કૃપા કરીને આજની અથવા ભવિષ્યની તારીખ પસંદ કરો.",
  },
  "Please select a valid demo time.": {
    hi: "कृपया मान्य डेमो समय चुनें।",
    gu: "કૃપા કરીને માન્ય ડેમો સમય પસંદ કરો.",
  },
  "Please describe what you want to explore.": {
    hi: "कृपया बताएं कि आप क्या एक्सप्लोर करना चाहते हैं।",
    gu: "કૃપા કરીને તમે શું જોવા માંગો છો તે લખો.",
  },
  success: {
    hi: "सफल",
    gu: "સફળ",
  },
};

const commonOptions = {
  buttonsStyling: false,
  customClass: {
    popup: "puresaas-alert-popup",
    confirmButton: "puresaas-alert-confirm",
    title: "puresaas-alert-title",
  },
  width: 420,
};

export function showSuccessAlert(message: string, titleOrOptions: string | AlertOptions = {}, options: AlertOptions = {}) {
  const { autoClose, locale, timer, title } = resolveAlertOptions(titleOrOptions, options);
  return Swal.fire({
    ...commonOptions,
    allowEscapeKey: autoClose ? false : undefined,
    allowOutsideClick: autoClose ? false : undefined,
    confirmButtonText: alertCopy[locale].confirm,
    icon: "success",
    showConfirmButton: autoClose ? false : undefined,
    timer: autoClose ? timer : undefined,
    timerProgressBar: autoClose ? true : undefined,
    title: translateAlertText(title ?? alertCopy[locale].success, locale),
    text: translateAlertText(message, locale),
  });
}

export function showErrorAlert(message: string, titleOrOptions: string | AlertOptions = {}, options: AlertOptions = {}) {
  const { locale, title } = resolveAlertOptions(titleOrOptions, options);
  return Swal.fire({
    ...commonOptions,
    confirmButtonText: alertCopy[locale].confirm,
    icon: "error",
    title: translateAlertText(title ?? alertCopy[locale].error, locale),
    text: translateAlertText(message, locale),
  });
}

export function showWarningAlert(message: string, titleOrOptions: string | AlertOptions = {}, options: AlertOptions = {}) {
  const { locale, title } = resolveAlertOptions(titleOrOptions, options);
  return Swal.fire({
    ...commonOptions,
    confirmButtonText: alertCopy[locale].confirm,
    icon: "warning",
    title: translateAlertText(title ?? alertCopy[locale].warning, locale),
    text: translateAlertText(message, locale),
  });
}

export const successAlert = showSuccessAlert;
export const errorAlert = showErrorAlert;
export const warningAlert = showWarningAlert;

function resolveAlertOptions(titleOrOptions: string | AlertOptions, options: AlertOptions) {
  const title = typeof titleOrOptions === "string" ? titleOrOptions : titleOrOptions.title;
  const locale = options.locale ?? (typeof titleOrOptions === "string" ? undefined : titleOrOptions.locale) ?? getCurrentLocale();
  const autoClose = options.autoClose ?? (typeof titleOrOptions === "string" ? undefined : titleOrOptions.autoClose) ?? false;
  const timer = options.timer ?? (typeof titleOrOptions === "string" ? undefined : titleOrOptions.timer) ?? 1400;

  return { autoClose, locale, timer, title };
}

function getCurrentLocale(): Locale {
  if (typeof window === "undefined") return defaultLocale;

  const pathLocale = window.location.pathname.split("/").filter(Boolean)[0];
  return pathLocale && hasLocale(pathLocale) ? pathLocale : defaultLocale;
}

function translateAlertText(text: string, locale: Locale) {
  if (locale === "en") return text;
  return messageTranslations[text]?.[locale] ?? text;
}
