import { DEFAULT_PAGE, DEFAULT_PAGE_SIZE_LIST_USAGERS, DEFAULT_SEARCH_QUERY } from "../app/config/constants";
import { apiFetch } from "../helpers/fetcher";

const BASE_URL = 'usagers';

const API_URL = `http://localhost:5000/${BASE_URL}`;

export async function createUsager(data: any) {
    const dataResult = await apiFetch(`${API_URL}/create`, {
        method: "POST",
        body: JSON.stringify(data)
    });
    return dataResult;
}

export async function updateUsager(id: string, data: any) {
    const result = await apiFetch(`${API_URL}/${id}`, {
        method: "PATCH",
        body: JSON.stringify(data)
    });
    return result;
}

export async function getUsagerById(id: string) {
    const dataResult = await apiFetch(`${API_URL}/${id}`);
    return dataResult;
}

export async function getUsagers(page = DEFAULT_PAGE, pageSize = DEFAULT_PAGE_SIZE_LIST_USAGERS,
    search: string = DEFAULT_SEARCH_QUERY) {
    const dataResult = await apiFetch(`${API_URL}?page=${page}&pageSize=${pageSize}&search=${encodeURIComponent(search)}`);
    return dataResult;
}

export async function getUsagersList() {
    const dataResult = await apiFetch(`${API_URL}/list`);
    return dataResult;
}