// app/(auth)/(tabs)/(account)/couponDetailPage.tsx
import { View, Text, ScrollView, Pressable, Alert } from 'react-native';
import React, { useContext, useEffect, useState } from 'react';
import { SafeAreaView } from 'react-native-safe-area-context';
import { router } from 'expo-router';
import { CrossLogoSvg } from '../../../../component/global/SVG';
import { AuthContext } from '../../../../context/AuthProvider';
import NormalInput from '../../../../component/global/normal_input';
import NormalButton from '../../../../component/global/normal_button';
import { authenticationService } from '../../../../service/authService';
import * as SecureStore from 'expo-secure-store';
import { useLocalSearchParams } from 'expo-router';
import { useTranslation } from '../../../../util/hooks/useTranslation';

const CouponDetailPage = () => {
    const { t } = useTranslation();
    const { user, setUser } = useContext(AuthContext);
    const [token, setToken] = useState<string | null>(null);
    const [name, setName] = useState<string | null>(null);
    const [isLoading, setIsLoading] = useState(false);
    const [error, setError] = useState<string | null>(null);

    const handleChangeName = async () => {
        if (!name) {
            setError(t('coupon_detail.enter_name_error'));
            return;
        }
        if (!token) {
            setError(t('coupon_detail.no_token_error'));
            return;
        }
        setError(null);
        setIsLoading(true);
        try {
            const success = await authenticationService.changeName(name, token);
            if (success) {
                if (user) {
                    setUser({
                        ...user,
                        nickname: name
                    });
                }
                router.replace('accountMainPage');
            } else {
                setError(t('coupon_detail.update_failed'));
            }
        } catch (error) {
            console.error('Error changing name:', error);
            setError(t('coupon_detail.general_error'));
        } finally {
            setIsLoading(false);
        }
    };
    const { couponName, couponDescription } = useLocalSearchParams();
    return (
        <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
            <ScrollView className="flex-1 mx-[5%]">
                <View style={{ marginTop: 25 }}>
                    <Pressable
                        onPress={() => {
                            if (router.canGoBack()) {
                                router.back();
                            } else {
                                router.replace('/accountMainPage');
                            }
                        }}
                    >
                        <CrossLogoSvg />
                    </Pressable>
                    <Text style={{ fontSize: 45, marginVertical: 25 }}>{t('coupon_detail.title')}</Text>
                    <Text className="text-2xl pb-4">{couponName}</Text>
                    <Text className="text-lg pb-8">{couponDescription}</Text>
                </View>
                <NormalButton
                    title={
                        <Text
                            style={{
                                color: 'white',
                                fontSize: 16,
                                fontWeight: '800'
                            }}
                        >
                            {t('coupon_detail.use_coupon')}
                        </Text>
                    }
                    onPress={() => {
                        Alert.alert(
                            t('coupon_detail.use_coupon'), // Title
                            t('coupon_detail.scan_qr_message'), // Message
                            [
                                {
                                    text: t('common.cancel'),
                                    style: 'cancel'
                                },
                                {
                                    text: t('common.confirm'),
                                    onPress: () => router.push('scanQrPage')
                                }
                            ]
                        );
                    }}
                    extendedStyle={{ padding: 24 }}
                />
            </ScrollView>
        </SafeAreaView>
    );
};

export default CouponDetailPage;