import { Certifications } from "./Certifications";
import { Employment } from "./Employment";
import { Testimonials } from "./Testimonials";

const getTimeBetween = (start: Date, end: Date) => {
  const months =
    (end.getFullYear() - start.getFullYear()) * 12 -
    start.getMonth() +
    end.getMonth();
  return Math.floor(months / 12) > 0
    ? `${Math.floor(months / 12)} years, ${months % 12} months`
    : `${months} months`;
};

export const Stats: {
  title: string;
  value: string | number;
}[] = [
  {
    title: "Current Positions Held",
    value: Employment.filter((el) => !el.end).length
  },
  {
    title: "Total Positions Held",
    value: Employment.length
  },
  {
    title: "Paid Positions Held",
    value: Employment.filter(
      (el) => el.type !== "hypothetical" && el.type !== "volunteer"
    ).length
  },
  {
    title: "Pro-Bono Positons Held",
    value: Employment.filter((el) => el.type === "volunteer").length
  },
  {
    title: "Current Longest Position Held",
    value: (() => {
      const longest = Employment.filter(
        (el) => !el.end && el.type !== "hypothetical"
      ).sort(
        (a, b) =>
          (b.end ?? new Date(Date.now())).getTime() -
          b.start.getTime() -
          ((a.end ?? new Date(Date.now())).getTime() - a.start.getTime())
      )[0];
      return `${longest.title} - ${getTimeBetween(longest.start, longest.end ?? new Date(Date.now()))}`;
    })()
  },
  {
    title: "All-time Longest Position Held",
    value: (() => {
      const longest = Employment.filter(
        (el) => el.type !== "hypothetical"
      ).sort(
        (a, b) =>
          (b.end ?? new Date(Date.now())).getTime() -
          b.start.getTime() -
          ((a.end ?? new Date(Date.now())).getTime() - a.start.getTime())
      )[0];
      return `${longest.title} - ${getTimeBetween(longest.start, longest.end ?? new Date(Date.now()))}`;
    })()
  },
  {
    title: "Clients Served",
    value: new Set(Employment.map((el) => el.company)).size
  },
  {
    title: "Reviews Received",
    value: Testimonials.length
  },
  {
    title: "Certifications Earned",
    value: Certifications.length
  }
];
