import axios from 'axios';
import * as SecureStore from 'expo-secure-store';
import { EXPO_PUBLIC_API_URL } from '@env';

class WalletService {
    private apiUrl: string;

    constructor() {
        this.apiUrl = EXPO_PUBLIC_API_URL;
        if (!this.apiUrl) {
            throw new Error('API URL is not defined in environment variables');
        }
    }
    async getCoupon() {
        try {
            const response = await axios.get(
                `${this.apiUrl}/clients/promotion/coupons`,
                {
                    headers: {
                        Authorization: `Bearer ${await SecureStore.getItemAsync(
                            'accessToken'
                        )}`
                    }
                }
            );

            if (response.status === 200) {
                console.log('getCoupon successful!');
                return response.data;
            } else {
                console.log('getCoupon failed:', response.status);
            }
        } catch (error) {
            console.log(error);
        }
    }
    async getWalletBalance() {
        try {
            const response = await axios.get(
                `${this.apiUrl}/clients/customer/wallet`,
                {
                    headers: {
                        Authorization: `Bearer ${await SecureStore.getItemAsync(
                            'accessToken'
                        )}`
                    }
                }
            );
            if (response.status === 200) {
                const walletBalance = response.data.data;
                // console.log(`i am wallet balance`, walletBalance)
                // console.log('getWalletBalance successful!');

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

    // async logout() {
    //     await SecureStore.deleteItemAsync('accessToken');
    //     console.log('log out successfully, accessToken deleted');
    // }
}
export const walletService = new WalletService();
