import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { institutionalExamService } from "@/services/institutional-exam.service";
import type { PaginationParams } from "@/types";
import type {
  InstExamSessionCreateRequest,
  InstExamCreateRequest,
  ResultTemplateCreateRequest,
  CourseAssignmentCreateRequest,
  SubjectRegistrationCreateRequest,
  AssignmentSubmission,
} from "@/types/institutional-exam";

export function useInstitutionalExamSessions(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.instExamSessions.list(params),
    queryFn: () => institutionalExamService.listSessions(params),
  });
}

export function useInstitutionalExamSession(id: number) {
  return useQuery({
    queryKey: queryKeys.instExamSessions.detail(id),
    queryFn: () => institutionalExamService.getSession(id),
    enabled: !!id,
  });
}

export function useExamTypes(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.examTypes.list(params),
    queryFn: () => institutionalExamService.listExamTypes(params),
  });
}

export function useGradeConfigs(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.gradeConfigs.list(params),
    queryFn: () => institutionalExamService.listGradeConfigs(params),
  });
}

export function useResultTemplates(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.resultTemplates.list(params),
    queryFn: () => institutionalExamService.listResultTemplates(params),
  });
}

export function useMarksheets(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.marksheets.list(params),
    queryFn: () => institutionalExamService.listMarksheets(params),
  });
}

export function useMarksheet(id: number) {
  return useQuery({
    queryKey: queryKeys.marksheets.detail(id),
    queryFn: () => institutionalExamService.getMarksheet(id),
    enabled: !!id,
  });
}

export function useCourseAssignments(params?: PaginationParams & { course_id?: number; batch_id?: number; state?: string }) {
  return useQuery({
    queryKey: queryKeys.courseAssignments.list(params),
    queryFn: () => institutionalExamService.listCourseAssignments(params),
  });
}

export function useCourseAssignment(id: number) {
  return useQuery({
    queryKey: queryKeys.courseAssignments.detail(id),
    queryFn: () => institutionalExamService.getCourseAssignment(id),
    enabled: !!id,
  });
}

export function useSubjectRegistrations(params?: PaginationParams & { student_id?: number; course_id?: number; state?: string }) {
  return useQuery({
    queryKey: queryKeys.subjectRegistrations.list(params),
    queryFn: () => institutionalExamService.listSubjectRegistrations(params),
  });
}

export function useAvailableSubjects(params?: { student_id?: number; course_id?: number }) {
  return useQuery({
    queryKey: queryKeys.subjectRegistrations.available(params),
    queryFn: () => institutionalExamService.getAvailableSubjects(params),
  });
}

export function useCreateExamSession() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: InstExamSessionCreateRequest) => institutionalExamService.createSession(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.instExamSessions.all }),
  });
}

export function useScheduleExamSession() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => institutionalExamService.scheduleSession(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.instExamSessions.all }),
  });
}

export function useCreateInstitutionalExam() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: InstExamCreateRequest) => institutionalExamService.createExam(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.instExamSessions.all }),
  });
}

export function useEnterMarks() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ examId, data }: { examId: number; data: { marks: { student_id: number; score: number }[] } }) =>
      institutionalExamService.enterMarks(examId, data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.instExamSessions.all }),
  });
}

export function useCreateResultTemplate() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: ResultTemplateCreateRequest) => institutionalExamService.createResultTemplate(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.resultTemplates.all }),
  });
}

export function useGenerateMarksheets() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (templateId: number) => institutionalExamService.generateMarksheets(templateId),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.marksheets.all }),
  });
}

export function useValidateMarksheet() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => institutionalExamService.validateMarksheet(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.marksheets.all }),
  });
}

export function useCreateCourseAssignment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: CourseAssignmentCreateRequest) => institutionalExamService.createCourseAssignment(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.courseAssignments.all }),
  });
}

export function usePublishCourseAssignment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => institutionalExamService.publishCourseAssignment(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.courseAssignments.all }),
  });
}

export function useSubmitAssignment() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ assignmentId, file }: { assignmentId: number; file: File }) =>
      institutionalExamService.submitAssignment(assignmentId, file),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.courseAssignments.all }),
  });
}

export function useGradeSubmission() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ submissionId, data }: { submissionId: number; data: Partial<AssignmentSubmission> }) =>
      institutionalExamService.gradeSubmission(submissionId, data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.courseAssignments.all }),
  });
}

export function useCreateSubjectRegistration() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: SubjectRegistrationCreateRequest) => institutionalExamService.createSubjectRegistration(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.subjectRegistrations.all }),
  });
}

export function useConfirmSubjectRegistration() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => institutionalExamService.confirmSubjectRegistration(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.subjectRegistrations.all }),
  });
}
