import React, { useState } from 'react'; import { View, Text, StyleSheet, TouchableOpacity, TextInput, ScrollView, ActivityIndicator, Alert, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router, useLocalSearchParams } from 'expo-router'; import { Ionicons } from '@expo/vector-icons'; import { AppColors, Spacing, BorderRadius, FontSizes, FontWeights } from '@/constants/theme'; import { api } from '@/services/api'; export default function ActivateScreen() { // Get params - beneficiaryId is REQUIRED (created in add-loved-one.tsx) // lovedOneName is for display purposes const params = useLocalSearchParams<{ lovedOneName?: string; beneficiaryId?: string }>(); const beneficiaryId = params.beneficiaryId ? parseInt(params.beneficiaryId, 10) : null; const lovedOneName = params.lovedOneName || ''; const [activationCode, setActivationCode] = useState(''); const [isActivating, setIsActivating] = useState(false); const [step, setStep] = useState<'code' | 'complete'>('code'); // Demo serial for testing without real hardware const DEMO_SERIAL = 'DEMO-00000'; const handleActivate = async () => { const code = activationCode.trim().toUpperCase(); if (!code) { Alert.alert('Error', 'Please enter serial number'); return; } // Check for demo serial const isDemoMode = code === DEMO_SERIAL || code === 'DEMO-1234-5678'; // Validate code format: minimum 8 characters (or demo serial) if (!isDemoMode && code.length < 8) { Alert.alert('Invalid Code', 'Please enter a valid serial number from your WellNuo Starter Kit.\n\nFor testing, use: DEMO-00000'); return; } // Beneficiary ID is required - was created in add-loved-one.tsx if (!beneficiaryId) { Alert.alert('Error', 'Beneficiary not found. Please go back and try again.'); return; } setIsActivating(true); try { // Call API to activate - sets has_existing_devices = true on backend const response = await api.activateBeneficiary(beneficiaryId, code); if (!response.ok) { Alert.alert('Error', response.error?.message || 'Failed to activate equipment'); return; } // Mark onboarding as completed await api.setOnboardingCompleted(true); setStep('complete'); } catch (error) { console.error('Failed to activate:', error); Alert.alert('Error', 'Failed to activate kit. Please try again.'); } finally { setIsActivating(false); } }; const handleComplete = () => { // Navigate to beneficiary detail page after activation if (beneficiaryId) { router.replace(`/(tabs)/beneficiaries/${beneficiaryId}` as const); } else { router.replace('/(tabs)'); } }; // Step 1: Enter activation code if (step === 'code') { return ( {/* Header */} router.back()}> Connect Sensors {/* Icon */} {/* Instructions */} {lovedOneName ? `Connect sensors for ${lovedOneName}` : 'Enter the serial number from your sensors'} {/* Input */} {/* Demo Code Link */} setActivationCode('DEMO-1234-5678')} > Use demo code {/* Activate Button */} {isActivating ? ( ) : ( Activate )} ); } // Step 2: Complete return ( {/* Success Icon */} Sensors Connected! Your sensors have been successfully connected for{' '} {lovedOneName || 'your loved one'} {/* Next Steps */} Next Steps: 1 Place sensors in your loved one's home 2 Connect the hub to WiFi 3 Subscribe to start monitoring ($49/month) {/* Complete Button */} Go to Dashboard ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: AppColors.background, }, content: { flex: 1, padding: Spacing.lg, }, header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', marginBottom: Spacing.xl, }, backButton: { padding: Spacing.sm, marginLeft: -Spacing.sm, }, title: { fontSize: FontSizes.xl, fontWeight: FontWeights.bold, color: AppColors.textPrimary, }, placeholder: { width: 40, }, iconContainer: { alignItems: 'center', marginBottom: Spacing.xl, }, instructions: { fontSize: FontSizes.base, color: AppColors.textSecondary, textAlign: 'center', marginBottom: Spacing.xl, lineHeight: 24, }, inputContainer: { marginBottom: Spacing.lg, }, input: { backgroundColor: AppColors.surface, borderRadius: BorderRadius.lg, paddingHorizontal: Spacing.lg, paddingVertical: Spacing.md, fontSize: FontSizes.lg, color: AppColors.textPrimary, textAlign: 'center', borderWidth: 1, borderColor: AppColors.border, }, demoCodeLink: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', gap: 4, marginBottom: Spacing.md, }, demoCodeLinkText: { fontSize: FontSizes.sm, color: AppColors.textMuted, }, primaryButton: { backgroundColor: AppColors.primary, paddingVertical: Spacing.lg, borderRadius: BorderRadius.lg, alignItems: 'center', marginTop: Spacing.md, }, buttonDisabled: { opacity: 0.7, }, primaryButtonText: { fontSize: FontSizes.lg, fontWeight: FontWeights.semibold, color: AppColors.white, }, successContainer: { flex: 1, alignItems: 'center', justifyContent: 'center', }, successIcon: { marginBottom: Spacing.xl, }, successTitle: { fontSize: FontSizes['2xl'], fontWeight: FontWeights.bold, color: AppColors.textPrimary, marginBottom: Spacing.md, }, successMessage: { fontSize: FontSizes.base, color: AppColors.textSecondary, textAlign: 'center', marginBottom: Spacing.md, }, beneficiaryHighlight: { fontWeight: FontWeights.bold, color: AppColors.primary, }, nextSteps: { width: '100%', backgroundColor: AppColors.surface, borderRadius: BorderRadius.lg, padding: Spacing.lg, }, nextStepsTitle: { fontSize: FontSizes.base, fontWeight: FontWeights.semibold, color: AppColors.textPrimary, marginBottom: Spacing.md, }, stepItem: { flexDirection: 'row', alignItems: 'center', gap: Spacing.md, marginBottom: Spacing.sm, }, stepNumber: { width: 24, height: 24, borderRadius: 12, backgroundColor: AppColors.primary, color: AppColors.white, textAlign: 'center', lineHeight: 24, fontSize: FontSizes.sm, fontWeight: FontWeights.bold, overflow: 'hidden', }, stepText: { fontSize: FontSizes.sm, color: AppColors.textSecondary, flex: 1, }, });