import { progressReports } from "./progress-report-data";
import { formatProgressReportDescription } from "./progress-report-calendar-mapper";
import type {
  ApiProgressReport,
  ApiProgressReportMinistryAssignment,
} from "./service/progress-report-service";
import {
  getDocumentUrl,
  type UploadedFileMetadata,
} from "@/lib/document-file";

export type ProgressReportMinistryRow = {
  id: number;
  ministryId?: number;
  name: string;
  logo: string | null;
  issueCount: number;
  attachment: UploadedFileMetadata | string | null;
};

export type ProgressReportDetail = {
  id: number;
  title: string;
  year: string | number;
  deadline: string;
  firstMeeting: string;
  secondMeeting: string;
  firstDeadline: string;
  secondDeadline: string;
  draftSemesterDocument: UploadedFileMetadata | null;
  reportDocument: UploadedFileMetadata | null;
  description: string;
  ministries: ProgressReportMinistryRow[];
};

export function mapApiProgressReportToDetail(
  report: ApiProgressReport,
): ProgressReportDetail {
  return {
    id: report.id,
    title: report.title,
    year: report.year,
    // These field names are offset from the slot names on purpose, because
    // that is how the detail page labels them:
    //   "Deadline"     -> FIRST slot
    //   "1st Deadline" -> SECOND slot
    //   "2nd Deadline" -> FINAL slot
    deadline: report.deadlines.firstDeadline?.date || "-",
    firstMeeting: report.meetings.firstMeeting?.meetingDate || "-",
    secondMeeting: report.meetings.secondMeeting?.meetingDate || "-",
    firstDeadline: report.deadlines.secondDeadline?.date || "-",
    secondDeadline: report.deadlines.finalDeadline?.date || "-",
    draftSemesterDocument: report.draftSemesterReport,
    reportDocument: report.attachment,
    description: formatProgressReportDescription(report.description),
    ministries: [],
  };
}

function getMinistryLogoUrl(value: string | null): string | null {
  const logoPath = value?.trim();

  if (!logoPath) return null;

  return logoPath.startsWith("/uploads/")
    ? getDocumentUrl(logoPath)
    : logoPath;
}

export function mapApiProgressReportMinistryToRow(
  assignment: ApiProgressReportMinistryAssignment,
): ProgressReportMinistryRow {
  return {
    id: assignment.id,
    ministryId: assignment.ministryId,
    name:
      assignment.ministry.description?.trim() || assignment.ministry.name,
    logo: getMinistryLogoUrl(assignment.ministry.logo),
    issueCount: assignment.issues,
    attachment: assignment.attachment,
  };
}

export function getMinistryRowAttachmentName(
  row: ProgressReportMinistryRow,
): string {
  if (!row.attachment) return "";
  return typeof row.attachment === "string"
    ? row.attachment
    : row.attachment.name;
}

export const progressReportMinistryRows: ProgressReportMinistryRow[] = [
  {
    id: 1,
    name: "Ministry of Agriculture Forestry and Fisheries",
    logo: "/images/logo/maff.png",
    issueCount: 3,
    attachment: "Attachment B",
  },
  {
    id: 2,
    name: "Ministry of Mines and Energy",
    logo: "/images/logo/mme.png",
    issueCount: 2,
    attachment: "Attachment C",
  },
  {
    id: 3,
    name: "National Banking of Cambodia",
    logo: "/images/logo/nbc.png",
    issueCount: 4,
    attachment: "Attachment A",
  },
  {
    id: 4,
    name: "Ministry of Education Youth and Sport",
    logo: "/images/logo/meys.png",
    issueCount: 1,
    attachment: "Attachment D",
  },
  {
    id: 5,
    name: "Ministry of Environment",
    logo: "/images/logo/moe.png",
    issueCount: 5,
    attachment: "Attachment E",
  },
  {
    id: 6,
    name: "Ministry of Labour and Vocational Training",
    logo: "/images/logo/mlvt.png",
    issueCount: 1,
    attachment: "Attachment F",
  },
  {
    id: 7,
    name: "Ministry of Tourism",
    logo: "/images/logo/tourism.png",
    issueCount: 4,
    attachment: "Attachment G",
  },
  {
    id: 8,
    name: "Ministry of Health",
    logo: "/images/logo/health.png",
    issueCount: 2,
    attachment: "Attachment H",
  },
  {
    id: 9,
    name: "Ministry of Industry, Science, Technology and Innovation",
    logo: "/images/logo/misti.png",
    issueCount: 1,
    attachment: "Attachment I",
  },
  {
    id: 10,
    name: "Ministry of Economy and Finance",
    logo: "/images/logo/mef.png",
    issueCount: 1,
    attachment: "Attachment J",
  },
  {
    id: 11,
    name: "Ministry of Public Works and Transport",
    logo: "/images/logo/mpwt.png",
    issueCount: 3,
    attachment: "Attachment K",
  },
  {
    id: 12,
    name: "Ministry of Commerce",
    logo: "/images/logo/commerce.png",
    issueCount: 1,
    attachment: "Attachment L",
  },
  {
    id: 13,
    name: "Ministry of Land Management, Urban Planning and Construction",
    logo: "/images/logo/mlmupc.png",
    issueCount: 3,
    attachment: "Attachment M",
  },
  {
    id: 14,
    name: "Non-Bank Financial Services Authority",
    logo: "/images/logo/nbfsa.png",
    issueCount: 2,
    attachment: "Attachment N",
  },
];

const progressReportDescription =
  "The private sector stated that cultivation is largely dependent on the weather (rain), and the private sector also observed that the Ministry of Water Resources and Meteorology often issues announcements regarding weather forecasting nationwide, which results in farmers in each region not receiving clear information.";

export function getProgressReportDetail(id: string): ProgressReportDetail {
  const reportId = Number(id);
  const report =
    progressReports.find((item) => item.id === reportId) ?? progressReports[0];

  return {
    id: report.id,
    title: report.title,
    year: report.year,
    // Same label offset as mapApiProgressReportToDetail above.
    deadline: report.deadline,
    firstMeeting: report.firstMeeting,
    secondMeeting: report.secondMeeting,
    firstDeadline: report.secondDeadline,
    secondDeadline: report.finalDeadline,
    draftSemesterDocument: report.draftSemesterDoc,
    reportDocument: report.reportDoc,
    description: progressReportDescription,
    ministries: progressReportMinistryRows,
  };
}
