import { http } from "./client";
import type { LoginRequest, LoginResponse, User } from "./types";

/** POST /api/auth/login — exchange credentials for a JWT + user record. */
export function login(credentials: LoginRequest): Promise<LoginResponse> {
  // skipAuthRedirect: a 401 here is "bad credentials", not "session expired".
  return http.post<LoginResponse>("/api/auth/login", credentials, {
    skipAuthRedirect: true,
  });
}

/** GET /api/auth/me — restore the current user from a stored token. */
export function getMe(): Promise<User> {
  return http.get<User>("/api/auth/me");
}

/** POST /api/auth/logout — revoke the current token server-side. */
export function logout(): Promise<{ message: string }> {
  return http.post<{ message: string }>("/api/auth/logout");
}
