import { DocumentItem } from "@/src/types/admin/types";
import { clsx, type ClassValue } from "clsx"
import JSZip from "jszip";
import { FaEnvelope, FaPhone, FaWhatsapp } from "react-icons/fa";
import { twMerge } from "tailwind-merge"

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

export function formatDate(dateString: string): string {
  const date = new Date(dateString);
  return date.toLocaleDateString("en-US", {
    year: "numeric",
    month: "long",
    day: "numeric",
  });
}

export function formatDateTime(dateString: string): string {
  const date = new Date(dateString);
  return date.toLocaleString("en-US", {
    year: "numeric",
    month: "long",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit",
  });
}


export const getContactIcon = (method: string) => {
  switch (method) {
    case "WhatsApp":
      return <FaWhatsapp className="text-green-600" size={18} />;
    case "Phone Call":
      return <FaPhone className="text-blue-600" size={18} />;
    case "Email":
      return <FaEnvelope className="text-purple-600" size={18} />;
    default:
      return <FaPhone className="text-gray-600" size={18} />;
  }
};

export const getDocumentUrl = (fileName: string): string => {
  return `${process.env.NEXT_PUBLIC_EXPRESS_BACKEND_PUBLIC || 'http://localhost:3000'}/documents/${fileName}`;
}

export const getAgreementUrl = (fileNames: string): string => {
  return `${process.env.NEXT_PUBLIC_EXPRESS_BACKEND_PUBLIC || 'http://localhost:3000'}/agreement/${fileNames}`;
}

export const getSiteVisitUrl = (fileNames: string): string => {
  return `${process.env.NEXT_PUBLIC_EXPRESS_BACKEND_PUBLIC || 'http://localhost:3000'}/site-visits/${fileNames}`;
}

export const getQuotationUrl = (fileNames: string): string => {
  return `${process.env.NEXT_PUBLIC_EXPRESS_BACKEND_PUBLIC || 'http://localhost:3000'}/quotations/${fileNames}`;
}

export const getMaintenancePhotoUrl = (photoUrl: string): string => {
  if (photoUrl.startsWith("/")) {
    return `${process.env.NEXT_PUBLIC_EXPRESS_BACKEND_PUBLIC || 'http://localhost:3000'}/${photoUrl}`;
  } 
  return `${process.env.NEXT_PUBLIC_EXPRESS_BACKEND_PUBLIC || 'http://localhost:3000'}/images/${photoUrl}`;
};

  export const getSafeFilename = (item: DocumentItem | { fileName: string; originalName?: string }): string => {
    return (
      (item as any).file_name ||
      (item as any).originalName ||
      "unnamed_file"
    );
  };

  export async function fetchBlobWithLogging(url: string | null | undefined, label: string): Promise<Blob | null> {
    if (!url) {
      console.warn(`${label}: No URL provided`);
      return null;
    }

    try {
      const response = await fetch(url);
      if (!response.ok) {
        console.warn(`${label}: HTTP ${response.status} — ${url}`);
        return null;
      }
      return await response.blob();
    } catch (err) {
      console.warn(`${label} failed:`, err);
      return null;
    }
  }

  export async function addFileToZip(
    zip: JSZip,
    url: string | null | undefined,
    zipPath: string,
    label: string,
  ): Promise<void> {
    const blob = await fetchBlobWithLogging(url, label);
    if (blob) {
      zip.file(zipPath, blob);
    }
  }