import { baseAPI } from "@/lib/api";

// --- Shapes returned by the draft-dashboard endpoints -----------------------
// These mirror the backend response one-to-one (see
// backend/src/modules/progress-reports/dashboards/dashboards.mapper.ts).

type ApiResponse<T> = {
  success: boolean;
  statusCode: number;
  message: string;
  data: T;
};

// "DRAFT" and "FINAL" numbers were saved by a Generate action. "LIVE" means
// the backend calculated them for this request only and saved nothing.
export type DraftDashboardStatus = "LIVE" | "DRAFT" | "FINAL";

export type DraftDashboardCards = {
  totalIssues: number;
  solved: number;
  inProgress: number;
  notAddressed: number;
  totalPrimaryAgencies: number;
  totalMinistries: number;
  // How the in-progress items split, judged by CDC-GPSF. Only the plenary
  // dashboard has these.
  midProgress: number | null;
  earlyProgress: number | null;
};

export type DraftDashboardWorkingGroupRow = {
  workingGroupId: number;
  workingGroupName: string;
  total: number;
  solved: number;
  inProgress: number;
  notAddressed: number;
};

export type DraftDashboardAgencyRow = {
  agencyId: number;
  agencyName: string;
  total: number;
  solved: number;
  inProgress: number;
  notAddressed: number;
};

export type DraftDashboardCategoryRow = {
  categoryId: number;
  categoryName: string;
  total: number;
  solved: number;
  inProgress: number;
  notAddressed: number;
};

// Which of the two saved dashboards a page is looking at.
export type DraftDashboardStage = "draft" | "final";

// Which dashboard: the working group one or the plenary one. They are counted
// from different things (issues vs RGC decisions) but saved and read the same
// way, so one service serves all four pages.
export type DraftDashboardBoard = "pswg" | "plenary";

// The final one lives under its own path, so the two never overwrite each
// other on the server either.
function dashboardPath(
  progressReportId: number,
  stage: DraftDashboardStage,
  board: DraftDashboardBoard,
) {
  const base = `/progress-reports/${progressReportId}/dashboards/${board}`;

  return stage === "final" ? `${base}/final` : base;
}

export type DraftDashboard = {
  type: "PSWG" | "PLENARY";
  status: DraftDashboardStatus;
  generatedAt: string | null;
  cards: DraftDashboardCards;
  byWorkingGroup: DraftDashboardWorkingGroupRow[];
  byPrimaryAgency: DraftDashboardAgencyRow[];
  byCategory: DraftDashboardCategoryRow[];
};

// Reads the PSWG draft (or final) dashboard of one progress report.
export async function getDraftDashboard(
  progressReportId: number,
  stage: DraftDashboardStage = "draft",
  board: DraftDashboardBoard = "pswg",
): Promise<DraftDashboard> {
  const response = (await baseAPI(
    dashboardPath(progressReportId, stage, board),
  )) as ApiResponse<DraftDashboard>;

  return response.data;
}

// "Live": publishes the saved final dashboard, so the Ministry, PSWG, CDC and
// CEFP dashboards all show these numbers.
export async function publishLiveDashboard(
  progressReportId: number,
  board: DraftDashboardBoard = "pswg",
): Promise<DraftDashboard> {
  const response = (await baseAPI(
    `/progress-reports/${progressReportId}/dashboards/${board}/live`,
    { method: "POST" },
  )) as ApiResponse<DraftDashboard>;

  return response.data;
}

// Recalculates the dashboard from the reports the ministries submitted and
// saves the result as a draft.
export async function generateDraftDashboard(
  progressReportId: number,
  stage: DraftDashboardStage = "draft",
  board: DraftDashboardBoard = "pswg",
): Promise<DraftDashboard> {
  const response = (await baseAPI(
    dashboardPath(progressReportId, stage, board),
    { method: "POST" },
  )) as ApiResponse<DraftDashboard>;

  return response.data;
}
