import { api, useCourse } from "@nice/client";
import { courseDetailSelect, CourseDto } from "@nice/common";
import React, { createContext, ReactNode, useState } from "react";
import { string } from "zod";

interface CourseDetailContextType {
	editId?: string; // 添加 editId
	course?: CourseDto;
	selectedLectureId?: string | undefined;
	setSelectedLectureId?: React.Dispatch<React.SetStateAction<string>>;
	isLoading?: boolean;
}
interface CourseFormProviderProps {
	children: ReactNode;
	editId?: string; // 添加 editId 参数
}
export const CourseDetailContext =
	createContext<CourseDetailContextType | null>(null);
export function CourseDetailProvider({
	children,
	editId,
}: CourseFormProviderProps) {
	const { data: course, isLoading }: { data: CourseDto; isLoading: boolean } =
		api.course.findFirst.useQuery(
			{
				where: { id: editId },
				include: {
					sections: { include: { lectures: true } },
					enrollments: true,
				},
			},
			{ enabled: Boolean(editId) }
		);
	const [selectedLectureId, setSelectedLectureId] = useState<
		string | undefined
	>(undefined);

	return (
		<CourseDetailContext.Provider
			value={{
				editId,
				course,
				selectedLectureId,
				setSelectedLectureId,
				isLoading,
			}}>
			{children}
		</CourseDetailContext.Provider>
	);
}
