import { api } from './api';
import { PageResponse, PageRequest } from '@/types/backend';
import { BankDTO } from '@/types/common';

const BANKS_URL = '/common/banks';

export const commonService = {
    // Bancos
    getBanks: async (pageRequest: PageRequest): Promise<PageResponse<BankDTO>> => {
        const { page = 0, size = 100, sortBy, sortDirection } = pageRequest;
        const params = new URLSearchParams();
        params.append('page', page.toString());
        params.append('size', size.toString());
        if (sortBy) {
            params.append('sortBy', sortBy);
            if (sortDirection) {
                params.append('sortDirection', sortDirection);
            }
        }

        const response = await api.get<PageResponse<BankDTO>>(BANKS_URL, { params });
        return response;
    },

    getBankById: async (id: string): Promise<BankDTO> => {
        const response = await api.get<BankDTO>(`${BANKS_URL}/${id}`);
        return response;
    },

    createBank: async (bank: BankDTO): Promise<BankDTO> => {
        const response = await api.post<BankDTO>(BANKS_URL, bank);
        return response;
    },

    updateBank: async (id: string, bank: BankDTO): Promise<BankDTO> => {
        const response = await api.put<BankDTO>(`${BANKS_URL}/${id}`, bank);
        return response;
    },
};
