export interface Certificate {
  id: number;
  certificate_no?: string;
  standard_name?: string;
  standard_short?: string;
  cert_type?: string;
  issue_date?: string;
  expire_date?: string;
  surveillance_audit_due?: string;
  recertification_due?: string;
  status?: string;
  scan_pdf_url?: string;
}

// e.g. "30 Jul 2027" instead of the browser-locale-dependent 7/30/2027
export function formatDate(dateStr?: string): string {
  if (!dateStr) return "—";
  const d = new Date(dateStr);
  if (isNaN(d.getTime())) return "—";
  const day = d.getDate();
  const month = d.toLocaleString("en-US", { month: "short" });
  const year = d.getFullYear();
  return day + " " + month + " " + year;
}

export function isExpiringSoon(expireDate?: string): boolean {
  if (!expireDate) return false;
  const days = Math.ceil(
    (new Date(expireDate).getTime() - Date.now()) / (1000 * 60 * 60 * 24)
  );
  return days > 0 && days <= 30;
}

export function isExpired(expireDate?: string): boolean {
  if (!expireDate) return false;
  return new Date(expireDate).getTime() < Date.now();
}

// NOTE: purple is reserved in the design system for active/selected states only
// (see dattabale.module.css header comment) - so certificate status never uses it.
export function statusMeta(cert: Certificate, styles: any): { label: string; badgeClass: string } {
  const raw = (cert.status || "").toLowerCase();

  // Real backend statuses take priority over date math - a cert marked
  // revoked or flagged is that, regardless of its expiry date.
  if (raw === "expired" || isExpired(cert.expire_date)) {
    return { label: "Expired", badgeClass: styles.statusDraft };
  }
  if (raw === "revoked") {
    return { label: "Revoked", badgeClass: styles.statusDraft };
  }
  if (raw === "fake") {
    return { label: "Flagged", badgeClass: styles.statusDraft };
  }
  if (raw === "superseded") {
    return { label: "Superseded", badgeClass: styles.statusCancelled };
  }
  if (raw === "pending_scan") {
    return { label: "Pending scan", badgeClass: styles.statusReview };
  }
  if (isExpiringSoon(cert.expire_date)) {
    return { label: "Expiring soon", badgeClass: styles.statusReview };
  }
  return { label: "Active", badgeClass: styles.statusCompleted };
}

// Mirrors the TYPE_CONFIG pattern from my-audits.mappers.ts - a colored pill
// per cert_type, same visual language as the audit type chips.
export function typeMeta(certType?: string): { bg: string; color: string; icon: string; label: string } {
  const t = (certType || "").toUpperCase();
  if (t === "INITIAL") return { bg: "#fef3c7", color: "#92400e", icon: "🏁", label: "Initial" };
  if (t === "SURVEILLANCE") return { bg: "#ffedd5", color: "#9a3412", icon: "👁", label: "Surveillance" };
  if (t === "RECERTIFICATION") return { bg: "#eef2ff", color: "#4338ca", icon: "🔄", label: "Recertification" };
  return { bg: "#f3f4f6", color: "#4b5563", icon: "•", label: certType || "—" };
}

export function getPageNumbers(current: number, total: number): (number | "...")[] {
  if (total <= 7) return Array.from({ length: total }, (_, i) => i + 1);
  const pages: (number | "...")[] = [1];
  if (current > 3) pages.push("...");
  for (let p = Math.max(2, current - 1); p <= Math.min(total - 1, current + 1); p++) {
    pages.push(p);
  }
  if (current < total - 2) pages.push("...");
  pages.push(total);
  return pages;
}
