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;
	isHeaderVisible: boolean; // 新增
	setIsHeaderVisible: (visible: boolean) => void; // 新增
}
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);
	const [isHeaderVisible, setIsHeaderVisible] = useState(true); // 新增
	return (
		<CourseDetailContext.Provider
			value={{
				editId,
				course,
				selectedLectureId,
				setSelectedLectureId,
				isLoading,
				isHeaderVisible,
				setIsHeaderVisible,
			}}>
			{children}
		</CourseDetailContext.Provider>
	);
}
