import { authClient, applyAuthHeader } from "@shared/api/client";

export interface LoginPayload {
  email: string;
  password: string;
}

export interface RegisterPayload {
  email: string;
  password: string;
}

export async function login(payload: LoginPayload) {
  const { data } = await authClient.post(
    "/api/v1/auth/login",
    {
      email: payload.email.trim(),
      password: payload.password
    },
    {
      headers: { "Content-Type": "application/json" }
    }
  );
  return data;
}

export async function register(payload: RegisterPayload) {
  const { data } = await authClient.post(
    "/api/v1/auth/register",
    {
      email: payload.email.trim(),
      password: payload.password
    },
    {
      headers: { "Content-Type": "application/json" }
    }
  );
  return data;
}

export async function logout(accessToken?: string | null) {
  const headers = accessToken ? applyAuthHeader({}) : {};
  await authClient.post("/api/v1/auth/logout", undefined, { headers });
}

export async function refresh() {
  const { data } = await authClient.post("/api/v1/auth/refresh");
  return data;
}

export async function verifyEmail(token: string) {
  const { data } = await authClient.post(
    "/api/v1/auth/verify-email",
    { token },
    { headers: { "Content-Type": "application/json" } }
  );
  return data;
}

export async function forgotPassword(email: string) {
  const { data } = await authClient.post(
    "/api/v1/auth/forgot-password",
    { email: email.trim() },
    { headers: { "Content-Type": "application/json" } }
  );
  return data;
}

export async function resetPassword(token: string, newPassword: string) {
  const { data } = await authClient.post(
    "/api/v1/auth/reset-password",
    { token, new_password: newPassword },
    { headers: { "Content-Type": "application/json" } }
  );
  return data;
}

export async function resendVerification(email: string) {
  const { data } = await authClient.post(
    "/api/v1/auth/resend-verification",
    { email: email.trim() },
    { headers: { "Content-Type": "application/json" } }
  );
  return data;
}
