import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "./keys";
import { admissionService } from "@/services/admission.service";
import type { PaginationParams } from "@/types";
import type {
  AdmissionRegisterCreateRequest,
  AdmissionCreateRequest,
} from "@/types/admission";

export function useAdmissionRegisters(params?: PaginationParams) {
  return useQuery({
    queryKey: queryKeys.admissionRegisters.list(params),
    queryFn: () => admissionService.listRegisters(params),
  });
}

export function useAdmissions(params?: PaginationParams & { state?: string; register_id?: number; course_id?: number }) {
  return useQuery({
    queryKey: queryKeys.admissions.list(params),
    queryFn: () => admissionService.listAdmissions(params),
  });
}

export function useAdmission(id: number) {
  return useQuery({
    queryKey: queryKeys.admissions.detail(id),
    queryFn: () => admissionService.getAdmission(id),
    enabled: !!id,
  });
}

export function useCreateAdmissionRegister() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: AdmissionRegisterCreateRequest) => admissionService.createRegister(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.admissionRegisters.all }),
  });
}

export function useConfirmRegister() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => admissionService.confirmRegister(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.admissionRegisters.all }),
  });
}

export function useCloseRegister() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => admissionService.closeRegister(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.admissionRegisters.all }),
  });
}

export function useCreateAdmission() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (data: AdmissionCreateRequest) => admissionService.createAdmission(data),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.admissions.all }),
  });
}

export function useSubmitAdmission() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => admissionService.submitAdmission(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.admissions.all }),
  });
}

export function useConfirmAdmission() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => admissionService.confirmAdmission(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.admissions.all }),
  });
}

export function useAdmitAdmission() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => admissionService.admitAdmission(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.admissions.all }),
  });
}

export function useRejectAdmission() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => admissionService.rejectAdmission(id),
    onSuccess: () => qc.invalidateQueries({ queryKey: queryKeys.admissions.all }),
  });
}
