import { AuthFormData, AuthMethod } from "../../types/auth";
import { useApiInterceptor } from "./useApiInterceptor";

// For webLogin
export async function useLogin({ email, password, isCandidate, loginMethod=AuthMethod.Email }: AuthFormData) {
    let response;
    try {
        response = await useApiInterceptor().post(`/login`, {
            email,
            password,
            profile: isCandidate ? 'Candidate' : 'Company',
            loginMethod 
        });
        console.log("response.status", response.status);
        if (response.status === 200) {
            const token = response.data.token;
            const refreshToken = response.data.refreshToken;
            const name = response.data.name;
            const profileType = response.data.profileType;
            const message = response.data.message;
            return {
                status: response.status,
                jwtToken: token,
                refreshToken,
                exception: "",
                message: message|| "Login Successful!",
                name,
                profileType
            };
        }
    } catch (e: any) {
        console.log("Error Occured in Login Hook", e);
        return {
            status: e.response.status,
            jwtToken: "",
            refreshToken: "",
            exception: e?.response?.data?.exception,
            message: e?.response?.data?.message
        };
    }
}