import { clearAuthToken, request, setAuthToken } from "./client";

export interface LoginResponse {
  token: string;
  expiresAt: string;
}

export interface AuthStatus {
  enabled: boolean;
}

export async function login(username: string, password: string): Promise<LoginResponse> {
  const result = await request<LoginResponse>("/auth/login", {
    method: "POST",
    body: { username, password },
    skipAuth: true
  });
  setAuthToken(result.token);
  return result;
}

export function signOut() {
  clearAuthToken();
}

export function setToken(token: string | null) {
  if (token) {
    setAuthToken(token);
  } else {
    clearAuthToken();
  }
}

export async function fetchAuthStatus(): Promise<AuthStatus> {
  return request<AuthStatus>("/auth/status", { method: "GET", skipAuth: true });
}
