import {
  SignInCredentials,
  SignInResponse,
  SignUpCredentials,
  SocialLoginCredentials,
} from "@/types/user";
import fetcher from "@/lib/fetcher";
import { DefaultResponse } from "@/types/global";
import { buildUrlQueryParams } from "@/utils/build-url-query-params";

export const authService = {
  socialLogin: async (body: SocialLoginCredentials) =>
    fetcher.post<DefaultResponse<SignInResponse>>(
      buildUrlQueryParams(`v1/auth/${body.type}/callback`, body.data)
    ),
  login: async (body: SignInCredentials) =>
    fetcher.post<DefaultResponse<SignInResponse>>("v1/auth/login", { body }),
  register: async (body: { phone: string }) =>
    fetcher.post<DefaultResponse<{ verifyId: string; phone: string; message?: string }>>(
      "v1/auth/register",
      { body }
    ),
  sendForgotPasswordSms: async (body: { phone: string }) =>
    fetcher.post<DefaultResponse<{ verifyId: string; phone: string; message?: string }>>(
      "v1/auth/forgot/password",
      { body }
    ),
  phoneSignUpComplete: async (body: SignUpCredentials) =>
    fetcher.post<DefaultResponse<SignInResponse>>("v1/auth/verify/phone", { body }),
  checkOtp: async (body: { verifyId: string; verifyCode: string; phone: string; type?: string }) =>
    fetcher.post<DefaultResponse<{ verifyId: string }>>("v1/auth/verify/otp", { body }),
  forgotPasswordPhone: async (body: { phone?: string; type: string; id?: string }) =>
    fetcher.post<DefaultResponse<SignInResponse>>("v1/auth/forgot/password/confirm", { body }),
  resetPasswordWithOtp: async (body: {
    phone: string;
    password: string;
    password_confirmation?: string | null;
    verifyId: string;
    verifyCode: string;
    type: string;
  }) =>
    fetcher.post<DefaultResponse<SignInResponse>>("v1/auth/forgot/password/confirm", { body }),
  logout: (data: { token: string }) => fetcher.post("v1/auth/logout", { body: data }),
};
