import { getAkun } from '@/api/api_akun';
import { getAnggota, getAnggotaById, getKabupaten, getKecamatan, getKelurahan } from '@/api/api_anggota';
import { getDashboardInfo } from '@/api/api_dashboard';
import { getDeposito, getDepositoById, getProdukDeposito, getTransaksiDeposito } from '@/api/api_deposito';
import { getKas, getKasById, getTransaksiKas } from '@/api/api_kas';
import { getBankById, getBanks, getPenempatan, getPenempatanById, getTransaksiPenempatan } from '@/api/api_penempatan_bank';
import { getProfil } from '@/api/api_profil';
import { getRoles } from '@/api/api_role';
import { getProdukTabungan, getTabungan, getTabunganById, getTransaksiTabungan } from '@/api/api_tabungan';
import { getUsers } from '@/api/api_user';
import { usePage } from '@inertiajs/react';
import { useQuery } from '@tanstack/react-query';

export const useRole = () => {
    const { token } = usePage().props;

    return useQuery({
        queryKey: ['role'],
        queryFn: async () => {
            const response = await getRoles(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useProdukTabungan = () => {
    const { token } = usePage().props;

    return useQuery({
        queryKey: ['produk_tabungan'],
        queryFn: () => getProdukTabungan(token as string),
    });
};

export const useTabungan = () => {
    const { token } = usePage().props;

    return useQuery({
        queryKey: ['tabungan'],
        queryFn: async () => {
            const response = await getTabungan(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useDetailTabungan = (id?: string) => {
    const { token } = usePage().props;

    return useQuery({
        queryKey: ['tabungan', id],
        queryFn: async () => {
            if (id) {
                const response = await getTabunganById(id, token as string);
                const json = await response.json();
                if (response.status == 200) {
                    return json;
                }
                throw new Error(json.message);
            }
            return null;
        },
    });
};

export const useProdukDeposito = () => {
    const { token } = usePage().props;

    return useQuery({
        queryKey: ['produk_deposito'],
        queryFn: () => getProdukDeposito(token as string),
    });
};

export const useDeposito = () => {
    const { token } = usePage().props;

    return useQuery({
        queryKey: ['deposito'],
        queryFn: async () => {
            const response = await getDeposito(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useDetailDeposito = (id?: string) => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['deposito', id],
        queryFn: async () => {
            if (id) {
                const response = await getDepositoById(id, token as string);
                const json = await response.json();
                if (response.status == 200) {
                    return json;
                }
                throw new Error(json.message);
            }
            return null;
        },
    });
};

export const useAnggota = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['anggota'],
        queryFn: async () => {
            const response = await getAnggota(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useDetailAnggota = (id?: string) => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['anggota', id],
        queryFn: async () => {
            if (id) {
                const response = await getAnggotaById(id, token as string);
                const json = await response.json();
                if (response.status == 200) {
                    return json;
                }
                throw new Error(json.message);
            }
            return null;
        },
    });
};

export const useAkun = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['akun'],
        queryFn: async () => {
            const response = await getAkun(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const usePenempatanBank = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['penempatan_bank'],
        queryFn: async () => {
            const response = await getPenempatan(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useKas = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['kas'],
        queryFn: async () => {
            const response = await getKas(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useDetailKas = (id?: string) => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['kas', id],
        queryFn: async () => {
            if (id) {
                const response = await getKasById(id, token as string);
                const json = await response.json();
                if (response.status == 200) {
                    return json;
                }
                throw new Error(json.message);
            }
            return null;
        },
    });
};

export const useDetailPenempatan = (id?: string) => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['penempatan_bank', id],
        queryFn: async () => {
            if (id) {
                const response = await getPenempatanById(id, token as string);
                const json = await response.json();
                if (response.status == 200) {
                    return json;
                }
                throw new Error(json.message);
            }
            return null;
        },
    });
};

export const useBank = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['bank'],
        queryFn: async () => {
            const response = await getBanks(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useDetailBank = (id: number) => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['bank', id],
        queryFn: async () => {
            const response = await getBankById(id, token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useTransaksiKas = (filter) => {
    const { token } = usePage().props;

    return useQuery({
        queryKey: ['transaksi_kas', filter.tgl_awal, filter.tgl_akhir],
        queryFn: async () => {
            const response = await getTransaksiKas(filter, token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useTransaksiTabungan = (filter) => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['transaksi_tabungan', filter.tgl_awal, filter.tgl_akhir, filter.rekening],
        queryFn: async () => {
            const response = await getTransaksiTabungan(filter, token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useTransaksiPenempatanBank = (filter) => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['transaksi_tabungan', filter.tgl_awal, filter.tgl_akhir, filter.rekening],
        queryFn: async () => {
            const response = await getTransaksiPenempatan(filter, token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useTransaksiDeposito = (filter) => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['transaksi_deposito', filter.tgl_awal, filter.tgl_akhir],
        queryFn: async () => {
            const response = await getTransaksiDeposito(filter, token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useProfil = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['profil'],
        queryFn: async () => {
            const response = await getProfil(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useUser = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['user'],
        queryFn: async () => {
            const response = await getUsers(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useKelurahan = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['kelurahan'],
        queryFn: async () => {
            const response = await getKelurahan(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useKecamatan = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['kecamatan'],
        queryFn: async () => {
            const response = await getKecamatan(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useKabupaten = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['kabupaten'],
        queryFn: async () => {
            const response = await getKabupaten(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};

export const useDashboardInfo = () => {
    const { token } = usePage().props;
    return useQuery({
        queryKey: ['dashboard_info'],
        queryFn: async () => {
            const response = await getDashboardInfo(token as string);
            const json = await response.json();
            if (response.status == 200) {
                return json;
            }
            throw new Error(json.message);
        },
    });
};
