

import { bagistoFetch, /*restApiFetch*/} from "@/utils/bagisto";
import { CUSTOMER_LOGIN } from "@/graphql/customer/mutations";


type LoginFormInputs = {
  username: string;
  password: string;
};
export default async function signInAuth(loginData:LoginFormInputs) {
    const input = {
        email: loginData.username,
        password: loginData.password,
    };
    const res = await bagistoFetch<any>({
        query: CUSTOMER_LOGIN,
        variables: { input },
        cache: "no-store",
    });
    const data = res?.body?.data?.createCustomerLogin?.customerLogin;
        

    if (!data || !data.success || !data.token) {
        throw new Error(data?.message || "Invalid credentials.");
    }

    return {
        id: data.id, // required by NextAuth
        email: loginData.username,
        name: loginData.username, // Using email as name since firstName/lastName are missing in response
        apiToken: data.apiToken,
        accessToken: data.token, // Sanctum token
        role: "customer",
    };
}