import { Deposito, ProdukDeposito, TransaksiDeposito } from "@/types";

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

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

export const saveDeposito = async (data: Deposito, token : string) => {
    const formRequest = {
        'anggota_id' : data.anggota?.id,
        'produk_id' : data.produk?.id,
        'tgl_jatuh_tempo' : data.tgl_jatuh_tempo,
        'nilai_valuta' : data.nilai_valuta,
        'jkw' : data.jkw,
        'suku_bunga' : data.suku_bunga,
        'tgl_valuta' : data.tgl_valuta,
        'nomor_bilyet' : data.nomor_bilyet,
        'rekening' : '',
        'rekening_tabungan' : data.rekening_tabungan,
    }
    if(data.rekening){
        formRequest.rekening = data.rekening;
        return await fetch(`/api/deposito/${data.rekening}`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(formRequest)
        });
    } else {
        return await fetch('/api/deposito', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(formRequest)
        });
    }
}

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

export async function saveProdukDeposito(prod : ProdukDeposito, token : string) {
    if(prod?.id){
        return await fetch(`/api/produk/deposito/${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/deposito', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify(prod)
        });
    }
}

export async function getTransaksiDeposito(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);

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

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

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

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