import type { DashboardHomeShellData, PerformanceBar } from "../shared/dashboard-types";
import { getDashboardAssignmentsHref, getDashboardHomeHref, getDashboardProgressHref } from "../../../lib/routes";
import { studentDashboardLabels } from "../../../content/dashboard-labels";
import { dashboardUiCopy } from "../../../content/ui-copy";
import { getDashboardHomeData } from "./dashboard-home.data";

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

type PracticeFocusCard = {
	title: string;
	description: string;
	meta: string;
	tone: "yellow" | "blue" | "teal";
	primaryLabel: string;
	primaryHref: string;
	secondaryLabel: string;
	secondaryHref: string;
};

export type DashboardPracticeData = {
	shell: DashboardHomeShellData;
	hero: {
		eyebrow: string;
		title: string;
		description: string;
		stats: PracticeFocusStat[];
	};
	cards: PracticeFocusCard[];
};

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

const findTopicBar = (items: PerformanceBar[], label: string) => items.find((item) => item.label === label) ?? null;

const summarizeTopicCoverage = (topic: PerformanceBar | null) => (topic ? `${topic.value}% reviewed in ${topic.label}` : "Short, focused practice is the fastest win.");

export const getDashboardPracticeData = async (studentId: number): Promise<DashboardPracticeData> => {
	const home = await getDashboardHomeData(studentId);
	const focusTopic = findSpotlightValue(home.hero.spotlightStats, studentDashboardLabels.spotlight.currentFocus, "Mixed practice");
	const bestTopic = findSpotlightValue(home.hero.spotlightStats, studentDashboardLabels.spotlight.bestTopic, "Building");
	const focusBar = findTopicBar(home.insights.topicMasteryBars, focusTopic);
	const bestBar = findTopicBar(home.insights.topicMasteryBars, bestTopic);
	const liveTargetTitle = home.hero.heroSideCard.title;
	const liveTargetHref = home.hero.heroSideCard.buttonHref;
	const liveTargetLabel = home.hero.heroSideCard.buttonLabel;
	const latestUpdate = home.hero.quickStats.find((item) => item.label === studentDashboardLabels.quickStats.latestUpdate)?.value ?? "No recent updates";

		return {
			shell: home.shell,
			hero: {
				eyebrow: dashboardUiCopy.studentPractice.eyebrow,
				title: dashboardUiCopy.studentPractice.title,
				description: `Use ${focusTopic.toLowerCase()} as the main repair point, keep one live assignment moving, and finish on ${bestTopic.toLowerCase()} so the session ends with momentum.`,
			stats: [
				{ label: studentDashboardLabels.practice.focusTopic, value: focusTopic },
				{ label: studentDashboardLabels.practice.topicCoverage, value: focusBar ? `${focusBar.value}%` : "--" },
				{ label: studentDashboardLabels.practice.bestTopic, value: bestTopic },
				{ label: studentDashboardLabels.practice.latestUpdate, value: latestUpdate },
			],
		},
		cards: [
				{
					title: `Rebuild ${focusTopic}`,
					description: `Start with the topic that still has the thinnest review coverage, then return straight to your live assignment while the method is fresh.`,
					meta: summarizeTopicCoverage(focusBar),
					tone: "yellow",
					primaryLabel: liveTargetLabel,
					primaryHref: liveTargetHref,
					secondaryLabel: dashboardUiCopy.studentPractice.buttons.viewProgress,
					secondaryHref: getDashboardProgressHref(),
				},
				{
					title: dashboardUiCopy.studentPractice.cards.firstPassTitle,
					description: `Answer one question on your own before switching into review mode. That gives you a stronger signal on what still needs support.`,
					meta: `Live target: ${liveTargetTitle}`,
					tone: "blue",
					primaryLabel: liveTargetLabel,
					primaryHref: liveTargetHref,
					secondaryLabel: dashboardUiCopy.studentPractice.buttons.seeAssignments,
					secondaryHref: getDashboardAssignmentsHref("student"),
				},
			{
				title: `Finish with ${bestTopic}`,
				description: `Once the harder practice block is done, close the session on a stronger area so the next study block starts with confidence.`,
					meta: bestBar ? `${bestBar.value}% reviewed in ${bestBar.label}` : "Pick a familiar topic for a strong finish.",
					tone: "teal",
					primaryLabel: dashboardUiCopy.studentPractice.buttons.seeAssignments,
					primaryHref: getDashboardAssignmentsHref("student"),
					secondaryLabel: dashboardUiCopy.studentPractice.buttons.openDashboard,
					secondaryHref: getDashboardHomeHref("student"),
				},
		],
	};
};
