import toast, { Toaster } from "react-hot-toast";

export const apiUrl = process.env.NEXT_PUBLIC_API_URL;
export const frontEndUrl = process.env.NEXT_PUBLIC_FRONTEND_URL;
export const siteName = process.env.NEXT_PUBLIC_SITE_NAME || "KYC";


export const adminEmail = process.env.NEXT_PUBLIC_ADMIN_EMAIL;

export const emailHost = process.env.NEXT_PUBLIC_EMAIL_HOST;
export const emailPort = parseInt(process.env.NEXT_PUBLIC_EMAIL_PORT, 10);
export const emailSecure = process.env.NEXT_PUBLIC_EMAIL_SECURE === "true";
export const emailUserName = process.env.NEXT_PUBLIC_EMAIL_USERNAME;
export const emailPassword = process.env.NEXT_PUBLIC_EMAIL_PASSWORD;
export const emailFrom = process.env.NEXT_PUBLIC_EMAIL_FROM;





export const copyright = `© ${new Date().getFullYear()} Upturnist. All rights reserved.`;

// Reusable Card Title component
export const CardTitle = ({ children }) => (
  <span className="text-[14px] sm:text-base font-semibold">{children}</span>
);

export const successMessage = (message) => {
  toast.dismiss();
  toast.success(message);
};

export const errorMessage = (message) => {
  toast.dismiss();
  toast.error(message);
};

export const formatPhone = (input) => {
  const str = input?.toString();
  return `${str.slice(0, 3)}   ${str.slice(3, 6)}   ${str.slice(6)}`;
};

//MAIL
export const mailResponse = async (
  to,
  subject,
  message,
  cc = "",
  attachments = []
) => {
  const formData = new FormData();
  formData.append("to", to);
  formData.append("subject", subject);
  formData.append("message", message);
  if (cc) formData.append("cc", cc);

  attachments.forEach((file) => {
    formData.append("attachments", file);
  });

  const baseUrl =
    process.env.NEXT_PUBLIC_FRONTEND_URL || window.location.origin;

  return await fetch(`${baseUrl}/api/mail`, {
    method: "POST",
    body: formData,
  });
};

export const getDateDifferenceInMinutes = (
  quoteDate,
  expiryDate,
  fallbackExpiry
) => {
  const normalizeDate = (d, isStart = false) => {
    if (!d) return null;
    if (typeof d === "number") return new Date(d); // treat as timestamp
    if (typeof d === "string")
      return new Date((isStart ? d : d) + (isStart ? "T00:00:00" : ""));
    return new Date(d); // fallback
  };

  const startDate = normalizeDate(
    quoteDate || new Date().toISOString().slice(0, 10),
    true
  );
  const endDate = normalizeDate(expiryDate || fallbackExpiry);

  const diffMs = endDate - startDate;
  return Math.floor(diffMs / (1000 * 60));
};


export const formattedDateWithTime = (dateString) => {
  if (!dateString) return "";

  const date = new Date(dateString);

  const formatted = date.toLocaleString("en-GB", {
    day: "2-digit",
    month: "short",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit",
    hour12: true,
  });

  // Capitalize AM/PM
  return formatted.replace(/(am|pm)/i, (match) => match.toUpperCase());
};

export const formatCleanText = (str) =>
  str
    ?.toString()
    .replace(/_/g, " ")
    .replace(/-/g, " ")
    .replace(/\b\w/g, (c) => c.toUpperCase()) || "";




   export  const countries = [
  "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina",
  "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados",
  "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina",
  "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia",
  "Cameroon", "Canada", "Central African Republic", "Chad", "Chile", "China", "Colombia", "Comoros",
  "Congo (Congo-Brazzaville)", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
  "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
  "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Eswatini",
  "Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", "Ghana",
  "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras",
  "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
  "Ivory Coast", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait",
  "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein",
  "Lithuania", "Luxembourg", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta",
  "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco",
  "Mongolia", "Montenegro", "Morocco", "Mozambique", "Myanmar (Burma)", "Namibia", "Nauru",
  "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "North Korea",
  "North Macedonia", "Norway", "Oman", "Pakistan", "Palau", "Palestine State", "Panama",
  "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
  "Romania", "Russia", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia",
  "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe",
  "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia",
  "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Korea", "South Sudan", "Spain",
  "Sri Lanka", "Sudan", "Suriname", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan",
  "Tanzania", "Thailand", "Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia",
  "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates",
  "United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City",
  "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe"
];
