import axios, { isAxiosError } from 'axios';
import * as SecureStore from 'expo-secure-store';
import {apiClient} from './requets'

class WalletService {
    async getCouponForSpecificUser(userID: string) {
        try {
            const response = await apiClient.instance.get(`/clients/promotion/coupons/${userID}`);
            if (response.status === 200 || response.status === 201) {
                return response.data;
            } else {
                return false;
            }
        } catch (error) {
            console.error('Error in getCouponForSpecificUser:', error);
            throw error;
        }
    }

    async getCustomerInfo() {
        try {
            const response = await apiClient.instance.get(`/clients/customer`);
            if (response.status === 200 || response.status === 201) {
                const info = response.data;
                return info;
            } else {
                console.error('get customer info failed:', response.status);
                return false;
            }
        } catch (error) {
            if (isAxiosError(error)) {
                console.error('get customer info error:', error.response?.data?.message || error.message);
            } else {
                console.error('An unexpected error occurred:', error);
            }
            return false;
        }
    }

    async getWalletBalance() {
        try {
            const response = await apiClient.instance.get(`/clients/customer/wallet`);
            if (response.status === 200) {
                const walletBalance = response.data.data;
                return walletBalance;
            } else {
                console.error('getWalletBalance failed:', response.status);
                return false;
            }
        } catch (error) {
            if (isAxiosError(error)) {
                console.error('getWallet error:', error.response?.data?.message || error.message);
            } else {
                console.error('An unexpected error occurred:', error);
            }
            return false;
        }
    }

    async getOutTradeNo() {
        try {
            const response = await apiClient.instance.get(`/clients/qfpay/out_trade_no`);
            if (response.status === 200) {
                const outTradeNo = response.data.out_trade_no;

                return outTradeNo;
            } else {
                console.error('get outTradeNo failed:', response.status);
                return false;
            }
        } catch (error) {
            if (isAxiosError(error)) {
                console.error('get outTradeNo error:', error.response?.data?.message || error.message);
            } else {
                console.error('An unexpected error occurred:', error);
            }
            return false;
        }
    }

    async getTransactionRecord() {
        try {
            const response = await apiClient.instance.get(`/clients/wallet/transaction/record`);
            if (response.status === 200) {
                const walletBalance = response.data;

                return walletBalance;
            } else {
                console.error('getWalletBalance failed:', response.status);
                return false;
            }
        } catch (error) {
            if (isAxiosError(error)) {
                console.error('getWallet error:', error.response?.data?.message || error.message);
            } else {
                console.error('An unexpected error occurred:', error);
            }
            return false;
        }
    }

    async submitPayment(
        stationID: string,
        connector: string,
        user: string,
        book_time: string,
        end_time: string,
        total_power: string,
        total_fee: string,
        promotion_code: string,
        car: string,
        type?: string,
        is_ic_call?: boolean
    ) {
        try {
            const payload = {
                stationID,
                connector,
                user,
                book_time,
                end_time,
                total_power,
                total_fee,
                promotion_code,
                car,
                type,
                is_ic_call
            };

            const response = await apiClient.instance.post(`/clients/pay`, payload);
            if (response.status === 200 || response.status === 201) {
                return response.data;
            } else {
                return false;
            }
        } catch (error) {
            return false;
        }
    }
    async newSubmitPayment(
        stationID: string,
        connector: string,
        user: string,
        book_time: string,
        end_time: string,
        total_power: number,
        total_fee: number,
        promotion_code: string[],
        with_coupon: boolean,
        car: string,
        type?: string,
        is_ic_call?: boolean
    ) {
        try {
            const payload = {
                stationID,
                connector,
                user,
                book_time,
                end_time,
                total_power,
                total_fee,
                promotion_code,
                with_coupon,
                car,
                type,
                is_ic_call
            };

            const response = await apiClient.instance.post(`/clients/pay`, payload);
            if (response.data.status === 200 || response.data.status === 201) {
                return response.data.status;
            } else {
                return response.data;
            }
        } catch (error) {
            if (isAxiosError(error)) {
                return {
                    error: true,
                    status: error.response?.status,
                    message: error.response?.data?.message || error.message
                };
            } else {
                return {
                    error: true,
                    message: 'An unexpected error occurred'
                };
            }
        }
    }

    async selectPaymentType() {
        try {
            const response = await apiClient.instance.get(`/clients/qfpay/type`);
            if (response.status === 200 || response.status === 201) {
                const info = response.data;
                return info;
            } else {
                console.error('get customer info failed:', response.status);
                return false;
            }
        } catch (error) {
            if (isAxiosError(error)) {
                console.error('get customer info error:', error.response?.data?.message || error.message);
            } else {
                console.error('An unexpected error occurred:', error);
            }
            return false;
        }
    }

    async submitPaymentAfterSelectingType(amount: number, pay_type: string, return_url?: string) {
        try {
            const response = await apiClient.instance.get(
                `/clients/qfpay/session?amount=${amount}&pay_type=${pay_type}&return_url=${return_url}`
            );
            // if (response.status === 200 || response.status === 201) {
            if (response) {
                const info = response.data;
                return info;
            } else {
                console.error('get customer info failed:', response);
                return false;
            }
        } catch (error) {
            if (isAxiosError(error)) {
                console.error('get customer info error:', error.response?.data?.message || error.message);
            } else {
                console.error('An unexpected error occurred:', error);
            }
            return false;
        }
    }
    async checkPaymentStatus(out_trade_no: string) {
        try {
            const response = await apiClient.instance.get(`/clients/qfpay/check/transaction/${out_trade_no}`);

            if (Array.isArray(response.data.data) && response.data.data.length > 0) {
                // If it's not empty, consider it a success and return the data
                return response.data.data;
            } else {
                // If it's an empty array, consider it false
                return false;
            }
        } catch (error) {
            if (isAxiosError(error)) {
                console.error('check payment status error:', error.response?.data?.message || error.message);
            } else {
                console.error('An unexpected error occurred:', error);
            }
            return false;
        }
    }
}
export const walletService = new WalletService();
