import React, { useState } from 'react'; import { View, Text, TouchableOpacity, StyleSheet, SafeAreaView, ScrollView, StatusBar, TextInput, Alert, ActivityIndicator, } from 'react-native'; import { LinearGradient } from 'expo-linear-gradient'; import { usePezkuwi } from '../contexts/PezkuwiContext'; import { submitKycApplication, uploadToIPFS, getCitizenshipStatus, } from '../../shared/lib/citizenship-workflow'; import { KurdistanColors } from '../theme/colors'; const BeCitizenScreen: React.FC = () => { const { api, selectedAccount } = usePezkuwi(); const [_isExistingCitizen, _setIsExistingCitizen] = useState(false); const [currentStep, setCurrentStep] = useState<'choice' | 'new' | 'existing'>('choice'); const [isSubmitting, setIsSubmitting] = useState(false); // New Citizen Form State const [fullName, setFullName] = useState(''); const [fatherName, setFatherName] = useState(''); const [motherName, setMotherName] = useState(''); const [tribe, setTribe] = useState(''); const [region, setRegion] = useState(''); const [email, setEmail] = useState(''); const [profession, setProfession] = useState(''); const [referralCode, setReferralCode] = useState(''); // Existing Citizen Login State const [citizenId, setCitizenId] = useState(''); const [password, setPassword] = useState(''); const handleNewCitizenApplication = async () => { if (!fullName || !fatherName || !motherName || !email) { Alert.alert('Error', 'Please fill in all required fields'); return; } if (!api || !selectedAccount) { Alert.alert('Error', 'Please connect your wallet first'); return; } setIsSubmitting(true); try { // Prepare citizenship data const citizenshipData = { fullName, fatherName, motherName, tribe, region, email, profession, referralCode, walletAddress: selectedAccount.address, timestamp: Date.now(), }; // Step 1: Upload encrypted data to IPFS const ipfsCid = await uploadToIPFS(citizenshipData); if (!ipfsCid) { throw new Error('Failed to upload data to IPFS'); } // Step 2: Submit KYC application to blockchain const result = await submitKycApplication( api, selectedAccount, fullName, email, ipfsCid, 'Citizenship application via mobile app' ); if (result.success) { Alert.alert( 'Application Submitted!', 'Your citizenship application has been submitted for review. You will receive a confirmation once approved.', [ { text: 'OK', onPress: () => { // Reset form setFullName(''); setFatherName(''); setMotherName(''); setTribe(''); setRegion(''); setEmail(''); setProfession(''); setReferralCode(''); setCurrentStep('choice'); }, }, ] ); } else { Alert.alert('Application Failed', result.error || 'Failed to submit application'); } } catch (error: unknown) { if (__DEV__) console.error('Citizenship application error:', error); Alert.alert('Error', error instanceof Error ? error.message : 'An unexpected error occurred'); } finally { setIsSubmitting(false); } }; const handleExistingCitizenLogin = async () => { if (!api || !selectedAccount) { Alert.alert('Error', 'Please connect your wallet first'); return; } setIsSubmitting(true); try { const status = await getCitizenshipStatus(api, selectedAccount.address); if (status.kycStatus === 'Approved' && status.hasCitizenTiki) { Alert.alert( 'Success', `Welcome back, Citizen!\n\nYour Tiki Number: ${status.tikiNumber || 'N/A'}`, [ { text: 'OK', onPress: () => { setCitizenId(''); setPassword(''); setCurrentStep('choice'); }, }, ] ); } else if (status.kycStatus === 'Approved' && !status.hasCitizenTiki) { Alert.alert( 'Almost there!', 'Your KYC is approved, but you haven\'t claimed your Citizen Tiki yet. Please claim it on the web portal.', [{ text: 'OK' }] ); } else if (status.kycStatus === 'Pending') { Alert.alert( 'Application Pending', 'Your citizenship application is still under review.', [{ text: 'OK' }] ); } else { Alert.alert( 'Not a Citizen', 'We couldn\'t find a citizenship record for this wallet. If you have a Citizen ID and Password, please note that wallet-based verification is now preferred.', [{ text: 'OK' }] ); } } catch (error: unknown) { if (__DEV__) console.error('Citizenship verification error:', error); Alert.alert('Error', 'Failed to verify citizenship status'); } finally { setIsSubmitting(false); } }; if (currentStep === 'choice') { return ( πŸ›οΈ Be a Citizen Join the Pezkuwi decentralized nation setCurrentStep('new')} activeOpacity={0.8} > πŸ“ New Citizen Apply for citizenship and join our community setCurrentStep('existing')} activeOpacity={0.8} > πŸ” Existing Citizen Access your citizenship account Citizenship Benefits βœ“ Voting rights in governance βœ“ Access to exclusive services βœ“ Referral rewards program βœ“ Community recognition ); } if (currentStep === 'new') { return ( setCurrentStep('choice')} > ← Back New Citizen Application Please provide your information to apply for citizenship Full Name * Father's Name * Mother's Name * Tribe Region Email * Profession Referral Code {isSubmitting ? ( ) : ( Submit Application )} ); } // Existing Citizen Login return ( setCurrentStep('choice')} > ← Back Citizen Verification Verify your status using your connected wallet Existing citizens are verified through their blockchain identity. Ensure your citizenship wallet is selected in the wallet tab. {isSubmitting ? ( ) : ( Verify Citizenship )} ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F5F5F5', }, gradient: { flex: 1, }, scrollContent: { flexGrow: 1, padding: 20, paddingTop: 60, }, header: { alignItems: 'center', marginBottom: 40, }, logoContainer: { width: 100, height: 100, borderRadius: 50, backgroundColor: KurdistanColors.spi, justifyContent: 'center', alignItems: 'center', marginBottom: 20, boxShadow: '0px 4px 8px rgba(0, 0, 0, 0.3)', elevation: 8, }, logoText: { fontSize: 48, }, title: { fontSize: 28, fontWeight: 'bold', color: KurdistanColors.spi, marginBottom: 8, }, subtitle: { fontSize: 16, color: KurdistanColors.spi, textAlign: 'center', opacity: 0.9, }, choiceContainer: { gap: 16, marginBottom: 40, }, choiceCard: { backgroundColor: KurdistanColors.spi, borderRadius: 20, padding: 24, alignItems: 'center', boxShadow: '0px 4px 8px rgba(0, 0, 0, 0.2)', elevation: 6, }, choiceIcon: { fontSize: 48, marginBottom: 16, }, choiceTitle: { fontSize: 20, fontWeight: 'bold', color: KurdistanColors.kesk, marginBottom: 8, }, choiceDescription: { fontSize: 14, color: '#666', textAlign: 'center', }, infoSection: { backgroundColor: 'rgba(255, 255, 255, 0.2)', borderRadius: 16, padding: 20, }, infoTitle: { fontSize: 18, fontWeight: '600', color: KurdistanColors.spi, marginBottom: 16, }, benefitItem: { flexDirection: 'row', alignItems: 'center', marginBottom: 12, }, benefitIcon: { fontSize: 16, color: KurdistanColors.spi, marginRight: 12, fontWeight: 'bold', }, benefitText: { fontSize: 14, color: KurdistanColors.spi, flex: 1, }, infoCard: { backgroundColor: `${KurdistanColors.kesk}15`, padding: 16, borderRadius: 12, marginBottom: 24, borderLeftWidth: 4, borderLeftColor: KurdistanColors.kesk, }, infoText: { fontSize: 14, color: KurdistanColors.reş, lineHeight: 20, opacity: 0.8, }, formContainer: { flex: 1, padding: 20, }, backButton: { marginBottom: 20, }, backButtonText: { fontSize: 16, color: KurdistanColors.kesk, fontWeight: '600', }, formTitle: { fontSize: 24, fontWeight: 'bold', color: KurdistanColors.reş, marginBottom: 8, }, formSubtitle: { fontSize: 14, color: '#666', marginBottom: 24, }, inputGroup: { marginBottom: 20, }, label: { fontSize: 14, fontWeight: '600', color: KurdistanColors.reş, marginBottom: 8, }, input: { backgroundColor: KurdistanColors.spi, borderRadius: 12, padding: 16, fontSize: 16, borderWidth: 1, borderColor: '#E0E0E0', }, submitButton: { backgroundColor: KurdistanColors.kesk, borderRadius: 12, padding: 16, alignItems: 'center', marginTop: 20, boxShadow: '0px 4px 6px rgba(0, 128, 0, 0.3)', elevation: 6, }, submitButtonDisabled: { opacity: 0.6, }, submitButtonText: { fontSize: 18, fontWeight: 'bold', color: KurdistanColors.spi, }, spacer: { height: 40, }, }); export default BeCitizenScreen;