import { ProdukTabungan, Tabungan, TransaksiTabungan } from "@/types";

export const getTabungan = async (token : string) => {
    return await fetch('/api/tabungan', {
        method : 'GET',
        headers : {
            'Content-Type' : 'application/json',
            'Accept' : 'application/json',
            'Authorization': `Bearer ${token}`
        }
    });
}

export const getTabunganById = async (id: string, token : string) => {
    return await fetch(`/api/tabungan/${id}`, {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}

export const saveTabungan = async (data: Tabungan, token : string) => {
    const formRequest = {
        'anggota_id' : data.anggota?.id,
        'produk_id' : data.produk?.id,
        'rekening' : '',
    }
    if(data?.rekening){
        formRequest.rekening = data.rekening;
        return await fetch(`/api/tabungan/${data.rekening}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(formRequest)
        });
    } else {
        return await fetch('/api/tabungan', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(formRequest)
        });
    }
}

export async function getProdukTabungan(token : string) {
    const response = await fetch('/api/produk/tabungan', {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
    const data = await response.json();
    return data;
}

export async function saveProdukTabungan(prod : ProdukTabungan, token : string) {
    if(prod?.id){
        return await fetch(`/api/produk/tabungan/${prod.id}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(prod)
        });
    } else {
        return await fetch('/api/produk/tabungan', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(prod)
        });
    }
}

export async function saveTransaksiTabungan(trans : TransaksiTabungan, token : string) {
    return await fetch('/api/transaksi/tabungan', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
        body: JSON.stringify(trans)
    });
}

export async function getTransaksiTabungan(filter, token : string) {
    const params = new URLSearchParams();
    if (filter.tgl_awal) params.append('tgl_awal', filter.tgl_awal);
    if (filter.tgl_akhir) params.append('tgl_akhir', filter.tgl_akhir);
    if (filter.rekening) params.append('rekening', filter.rekening);

    return await fetch(`/api/transaksi/tabungan?${params.toString()}`, {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}

export async function deleteTransaksiTabungan(transId : string, token : string) {
    return await fetch(`/api/transaksi/tabungan/${transId}`, {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}

export async function validasiTransaksiTabungan(transId : string, token : string) {
    return await fetch(`/api/transaksi/tabungan/${transId}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': `Bearer ${token}`
        },
    });
}
