import type { BadgeTone } from "@/components/ui/badge";

export type ProjectState = "À venir" | "Actif" | "En standby" | "Fini";

export const projectStateOptions: ProjectState[] = ["À venir", "Actif", "En standby", "Fini"];

export const projectStateTones: Record<ProjectState, BadgeTone> = {
  "À venir": "blue",
  Actif: "green",
  "En standby": "orange",
  Fini: "neutral"
};

export type ProjectStateSource = {
  startsAt?: string;
  endsAt?: string;
  defaultState?: ProjectState;
};

const currentDate = new Date(2026, 5, 19);

export function getDefaultProjectState(project: ProjectStateSource): ProjectState {
  if (project.defaultState) {
    return project.defaultState;
  }

  const startsAt = parseProjectDate(project.startsAt);
  const endsAt = parseProjectDate(project.endsAt);

  if (startsAt && startsAt > currentDate) {
    return "À venir";
  }

  if (endsAt && endsAt < currentDate) {
    return "Fini";
  }

  return "Actif";
}

function parseProjectDate(value?: string) {
  if (!value) {
    return undefined;
  }

  const date = new Date(value);

  return Number.isNaN(date.getTime()) ? undefined : date;
}
