import { signInWithPassword } from "@/lib/auth"
import { useMutation } from "@tanstack/react-query"
import { useNavigate, useParams } from "@tanstack/react-router"

export const useBackOfficeLogin = () => {
  const navigate = useNavigate()
  const { lang } = useParams({ strict: false }) as { lang?: string }
  return useMutation({
    mutationFn: async ({ email, password }: { email: string; password: string }) => {
      return await signInWithPassword(email, password)
    },
    onSuccess: () => {
      navigate({ to: `/${lang || 'en'}/backoffice/candidates`, replace: true })
    },
    onError: (error) => {
      console.error('BackOffice login failed:', error)
    }
  })
}
