import { API_BASE, ADMIN_API_PREFIX } from "./config";

export async function adminLogin(username: string, password: string) {
    const body = new URLSearchParams({ username, password });
    const res = await fetch(`${API_BASE}${ADMIN_API_PREFIX}/auth/login`, {
        method: "POST",
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body,
    });
    if (!res.ok) throw new Error(await res.text().catch(()=>res.statusText));
    return res.json() as Promise<{ access_token: string }>;
}

export async function adminListBoards(token: string) {
    const res = await fetch(`${API_BASE}${ADMIN_API_PREFIX}/boards`, {
        headers: { Authorization: `Bearer ${token}` },
    });
    if (!res.ok) throw new Error(await res.text().catch(()=>res.statusText));
    return res.json();
}

export async function adminListUsers(token: string) {
    const res = await fetch(`${API_BASE}${ADMIN_API_PREFIX}/members`, {
        headers: { Authorization: `Bearer ${token}` },
    });
    if (!res.ok) throw new Error(await res.text().catch(()=>res.statusText));
    return res.json();
}