// Data types and filter helpers for the CDC-GPSF progress-report page.
// The list screen receives its rows from the progress-report API.

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

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

// Which of the two page views is showing: the table or the calendar.
export type ProgressReportView = "reports" | "calendar";

// The three filter controls at the top of the page.
export type ProgressReportFiltersValue = {
  semesters: string[];
  year: string;
  statuses: string[];
};

// One row of the progress-report table.
export type ProgressReportRow = {
  id: number;
  title: string;
  description: string;
  semester: "S1" | "S2";
  year: string;
  deadline: string;
  // Ids of the deadline rows, used when a date is edited instead of added.
  firstDeadlineId: number | null;
  secondDeadlineId: number | null;
  finalDeadlineId: number | null;
  firstDeadlineStatus: "DRAFT" | "SENT" | null;
  secondDeadlineStatus: "DRAFT" | "SENT" | null;
  finalDeadlineStatus: "DRAFT" | "SENT" | null;
  firstMeeting: string;
  firstMeetingStatus: "DRAFT" | "SENT" | null;
  secondDeadline: string;
  secondMeeting: string;
  secondMeetingStatus: "DRAFT" | "SENT" | null;
  finalDeadline: string;
  draftSemesterDoc: UploadedFileMetadata | null;
  reportDoc: UploadedFileMetadata | null;
  status: string;
};

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

// These mock rows remain available only for the separate mocked detail page.
export const progressReports: ProgressReportRow[] = [
  {
    id: 1,
    title: "Semester 2",
    description: "",
    semester: "S2",
    year: "2025",
    deadline: "Oct 30, 2025",
    firstDeadlineId: null,
    secondDeadlineId: null,
    finalDeadlineId: null,
    firstDeadlineStatus: "SENT",
    secondDeadlineStatus: "SENT",
    finalDeadlineStatus: "SENT",
    firstMeeting: "Jun 07, 2025",
    firstMeetingStatus: "SENT",
    secondDeadline: "Jun 30, 2025",
    secondMeeting: "Aug 20, 2025",
    secondMeetingStatus: "SENT",
    finalDeadline: "Sep 21, 2025",
    draftSemesterDoc: null,
    reportDoc: null,
    status: "Sent",
  },
  {
    id: 2,
    title: "Semester 1",
    description: "",
    semester: "S1",
    year: "2025",
    deadline: "Apr 30, 2025",
    firstDeadlineId: null,
    secondDeadlineId: null,
    finalDeadlineId: null,
    firstDeadlineStatus: "SENT",
    secondDeadlineStatus: "SENT",
    finalDeadlineStatus: "SENT",
    firstMeeting: "Jan 13, 2025",
    firstMeetingStatus: "SENT",
    secondDeadline: "Feb 02, 2025",
    secondMeeting: "Feb 14, 2025",
    secondMeetingStatus: "SENT",
    finalDeadline: "Apr 20, 2025",
    draftSemesterDoc: null,
    reportDoc: null,
    status: "Sent",
  },
];

// Keep only the rows that match every selected filter.
// An empty filter (nothing selected) matches everything.
export function filterProgressReportRows(
  rows: ProgressReportRow[],
  filters: ProgressReportFiltersValue,
) {
  return rows.filter((row) => {
    const matchesSemester =
      filters.semesters.length === 0 ||
      filters.semesters.includes(row.semester);
    const matchesYear = !filters.year || row.year === filters.year;
    const matchesStatus =
      filters.statuses.length === 0 || filters.statuses.includes(row.status);

    return matchesSemester && matchesYear && matchesStatus;
  });
}

// Build the dropdown options from the rows so the filters always
// offer exactly the values that exist in the table.
export function getProgressReportFilterOptions(rows: ProgressReportRow[]) {
  const semesters = Array.from(new Set(rows.map((row) => row.semester))).sort();
  const years = Array.from(new Set(rows.map((row) => row.year))).sort(
    (first, second) => Number(second) - Number(first),
  );
  const statuses = Array.from(new Set(rows.map((row) => row.status))).sort();

  return {
    semesters: semesters.map<ProgressReportOption>((semester) => ({
      label: semester === "S1" ? "Semester 1" : "Semester 2",
      value: semester,
    })),
    years: years.map<ProgressReportOption>((year) => ({
      label: year,
      value: year,
    })),
    statuses: statuses.map<ProgressReportOption>((status) => ({
      label: status,
      value: status,
    })),
  };
}
