import {Module} from "@/interfaces";
import {Exam, UserSolution} from "@/interfaces/exam";
import {Assignment} from "@/interfaces/results";
import {create} from "zustand";

export interface ExamState {
	exams: Exam[];
	setExams: (exams: Exam[]) => void;

	userSolutions: UserSolution[];
	setUserSolutions: (userSolutions: UserSolution[]) => void;

	showSolutions: boolean;
	setShowSolutions: (showSolutions: boolean) => void;

	hasExamEnded: boolean;
	setHasExamEnded: (hasExamEnded: boolean) => void;

	selectedModules: Module[];
	setSelectedModules: (modules: Module[]) => void;

	assignment?: Assignment;
	setAssignment: (assignment: Assignment) => void;

	timeSpent: number;
	setTimeSpent: (timeSpent: number) => void;

	sessionId: string;
	setSessionId: (sessionId: string) => void;

	moduleIndex: number;
	setModuleIndex: (moduleIndex: number) => void;

	exam?: Exam;
	setExam: (exam?: Exam) => void;

	reset: () => void;
}

export const initialState = {
	exams: [],
	userSolutions: [],
	showSolutions: false,
	selectedModules: [],
	hasExamEnded: false,
	assignment: undefined,
	timeSpent: 0,
	sessionId: "",
	exam: undefined,
	moduleIndex: 0,
};

const useExamStore = create<ExamState>((set) => ({
	...initialState,

	setUserSolutions: (userSolutions: UserSolution[]) => set(() => ({userSolutions})),
	setExams: (exams: Exam[]) => set(() => ({exams})),
	setShowSolutions: (showSolutions: boolean) => set(() => ({showSolutions})),
	setSelectedModules: (modules: Module[]) => set(() => ({selectedModules: modules})),
	setHasExamEnded: (hasExamEnded: boolean) => set(() => ({hasExamEnded})),
	setAssignment: (assignment: Assignment) => set(() => ({assignment})),
	setTimeSpent: (timeSpent) => set(() => ({timeSpent})),
	setSessionId: (sessionId: string) => set(() => ({sessionId})),
	setExam: (exam?: Exam) => set(() => ({exam})),
	setModuleIndex: (moduleIndex: number) => set(() => ({moduleIndex})),

	reset: () => set(() => initialState),
}));

export default useExamStore;
