import type { UploadedFileMetadata } from "@/lib/document-file";

export type ProgressReportOption = {
  label: string;
  value: string;
};

export type ProgressReportFiltersValue = {
  year: string;
  statuses: string[];
};

export type ProgressReportRow = {
  id: number;
  title: string;
  year: string;
  deadline: string;
  firstMeeting: string;
  secondDeadline: string;
  secondMeeting: string;
  thirdDeadline: string;
  draftSemester: UploadedFileMetadata | null;
  reportDoc: UploadedFileMetadata | null;
  status: string;
};

export const defaultProgressReportFilters: ProgressReportFiltersValue = {
  year: "",
  statuses: [],
};

// This mock array only backs the (out-of-scope) detail page's lookup by id
// today — the list screen now fetches real data. draftSemester/reportDoc
// are null here since nothing reads them from this array anymore.
export const progressReports: ProgressReportRow[] = [
  {
    id: 1,
    title: "Semester 2",
    year: "2025",
    deadline: "2025-10-30",
    firstMeeting: "2025-06-07",
    secondDeadline: "2025-06-30",
    secondMeeting: "2025-08-20",
    thirdDeadline: "2025-09-21",
    draftSemester: null,
    reportDoc: null,
    status: "Sent",
  },
  {
    id: 2,
    title: "Semester 1",
    year: "2025",
    deadline: "2025-04-30",
    firstMeeting: "2025-01-13",
    secondDeadline: "2025-02-02",
    secondMeeting: "2025-02-14",
    thirdDeadline: "2025-04-20",
    draftSemester: null,
    reportDoc: null,
    status: "Draft",
  },
];

export function filterProgressReportRows(
  rows: ProgressReportRow[],
  filters: ProgressReportFiltersValue,
) {
  return rows.filter((row) => {
    const matchesYear = !filters.year || row.year === filters.year;
    const matchesStatus =
      filters.statuses.length === 0 || filters.statuses.includes(row.status);

    return matchesYear && matchesStatus;
  });
}

export function getProgressReportFilterOptions(rows: ProgressReportRow[]) {
  const years = Array.from(new Set(rows.map((row) => row.year))).sort(
    (first, second) => Number(second) - Number(first),
  );
  // Derive status options from the rows so the filter always matches the
  // workflow statuses actually shown in the table.
  const statuses = Array.from(new Set(rows.map((row) => row.status))).sort();

  return {
    statuses: statuses.map<ProgressReportOption>((status) => ({
      label: status,
      value: status,
    })),
    years: years.map<ProgressReportOption>((year) => ({
      label: year,
      value: year,
    })),
  };
}
