import type { DashboardActivityData, DashboardHomeShellData, DashboardInsightsData } from "../shared/dashboard-types";
import { studentDashboardLabels } from "../../../content/dashboard-labels";
import { dashboardUiCopy } from "../../../content/ui-copy";
import { getDashboardHomeData } from "./dashboard-home.data";

type ProgressFocusStat = {
	label: string;
	value: string;
};

export type DashboardProgressData = {
	shell: DashboardHomeShellData;
	hero: {
		eyebrow: string;
		title: string;
		description: string;
		stats: ProgressFocusStat[];
	};
	activity: DashboardActivityData;
	insights: DashboardInsightsData;
};

const findSpotlightValue = (items: { label: string; value: string }[], label: string, fallback: string) =>
	items.find((item) => item.label === label)?.value ?? fallback;

const findQuickStatValue = (items: { label: string; value: string }[], label: string, fallback: string) =>
	items.find((item) => item.label === label)?.value ?? fallback;

export const getDashboardProgressData = async (studentId: number): Promise<DashboardProgressData> => {
	const home = await getDashboardHomeData(studentId);
	const currentFocus = findSpotlightValue(home.hero.spotlightStats, studentDashboardLabels.spotlight.currentFocus, "Mixed practice");
	const strongestTopic = findSpotlightValue(home.hero.spotlightStats, studentDashboardLabels.spotlight.bestTopic, "Building");
	const latestUpdate = findQuickStatValue(home.hero.quickStats, studentDashboardLabels.quickStats.latestUpdate, "No recent updates");
	const answerCoverage = home.activity.highlightCards[1]?.value ?? "0%";

		return {
			shell: home.shell,
			hero: {
				eyebrow: dashboardUiCopy.studentProgress.eyebrow,
				title: dashboardUiCopy.studentProgress.title,
				description: `Track what has been reviewed, see how much of your question load is active, and use ${currentFocus.toLowerCase()} as the fastest next improvement area.`,
			stats: [
				{ label: studentDashboardLabels.progress.reviewed, value: `${home.insights.overallPassRate}%` },
				{ label: studentDashboardLabels.progress.coverage, value: answerCoverage },
				{ label: studentDashboardLabels.progress.strongest, value: strongestTopic },
				{ label: studentDashboardLabels.progress.latestUpdate, value: latestUpdate },
			],
		},
		activity: home.activity,
		insights: home.insights,
	};
};
