import { api } from '@/core/services/api'
import type {
    BankDetailApiResponse,
    BankDetailPayload,
    BankDetailsApiResponse,
} from '../types/bankDetail.types'

export async function listBankDetails(perPage = 100): Promise<BankDetailsApiResponse> {
    const { data } = await api.get<BankDetailsApiResponse>('/v1/bank_details', {
        params: { per_page: perPage },
    })

    if (!data.success) {
        throw new Error(data.message ?? 'Gagal memuatkan akaun bank.')
    }

    return data
}

export async function createBankDetail(payload: BankDetailPayload): Promise<BankDetailApiResponse> {
    const { data } = await api.post<BankDetailApiResponse>('/v1/bank_details', payload)

    if (!data.success) {
        throw new Error(data.message ?? 'Gagal menambah akaun bank.')
    }

    return data
}

export async function updateBankDetail(
    id: string,
    payload: BankDetailPayload,
): Promise<BankDetailApiResponse> {
    const { data } = await api.put<BankDetailApiResponse>(`/v1/bank_details/${id}`, payload)

    if (!data.success) {
        throw new Error(data.message ?? 'Gagal mengemas kini akaun bank.')
    }

    return data
}

export async function deleteBankDetail(id: string): Promise<BankDetailApiResponse> {
    const { data } = await api.delete<BankDetailApiResponse>(`/v1/bank_details/${id}`)

    if (!data.success) {
        throw new Error(data.message ?? 'Gagal memadam akaun bank.')
    }

    return data
}
