import { useMemo, useState } from "react"; import { ActivityIndicator, Alert, View } from "react-native"; import { router, useLocalSearchParams } from "expo-router"; import * as LocalAuthentication from "expo-local-authentication"; import { useTranslation } from "react-i18next"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { Button } from "@/components/ui/Button"; import { Field, SwitchRow } from "@/components/ui/Field"; import { Card } from "@/components/ui/Card"; import { AppHeader } from "@/components/ui/Header"; import { Screen } from "@/components/ui/Screen"; import { Text } from "@/components/ui/Text"; import { DEMO_OTP } from "@/lib/commerce"; import { colors, gutter } from "@/lib/theme"; import { routes } from "@/lib/routes"; import { useAuthStore, type AuthProvider, } from "@/lib/stores/authStore"; const DEMO_EMAIL_PASSWORD = "gishen123"; const METHOD_META: Record< AuthProvider, { title: string; subtitle: string } > = { google: { title: "Google", subtitle: "Connect with your Google account (demo).", }, email: { title: "Email", subtitle: `Use any email. Demo password is ${DEMO_EMAIL_PASSWORD}.`, }, phone: { title: "Phone", subtitle: `We'll send an OTP. Demo code is always ${DEMO_OTP}.`, }, telegram: { title: "Telegram", subtitle: "Confirm with your Telegram username (demo).", }, guest: { title: "Guest", subtitle: "Browse without an account.", }, }; function normalizeMethod(raw: string | string[] | undefined): AuthProvider { const v = Array.isArray(raw) ? raw[0] : raw; if (v === "google" || v === "email" || v === "phone" || v === "telegram") { return v; } return "phone"; } export default function OtpScreen() { const { t } = useTranslation(); const insets = useSafeAreaInsets(); const params = useLocalSearchParams<{ method?: string }>(); const method = useMemo(() => normalizeMethod(params.method), [params.method]); const meta = METHOD_META[method]; const signIn = useAuthStore((s) => s.signIn); const setHasOnboarded = useAuthStore((s) => s.setHasOnboarded); const setBiometric = useAuthStore((s) => s.setBiometric); const [name, setName] = useState("Kirukib Abebe"); const [phone, setPhone] = useState("+251911000000"); const [email, setEmail] = useState("kirukib@gmail.com"); const [password, setPassword] = useState(""); const [telegram, setTelegram] = useState("@kirukib"); const [code, setCode] = useState(""); const [sent, setSent] = useState(false); const [bio, setBio] = useState(false); const [busy, setBusy] = useState(false); const finishSignIn = async (payload: { name: string; phone: string; email?: string; }) => { if (bio) { const hw = await LocalAuthentication.hasHardwareAsync(); if (hw) { const res = await LocalAuthentication.authenticateAsync({ promptMessage: "Enable biometric unlock", }); if (!res.success) { Alert.alert("Biometric", "Skipped — you can enable later in profile."); } } } signIn({ name: payload.name.trim(), phone: payload.phone.trim(), email: payload.email?.trim() || undefined, isGuest: false, authProvider: method, }); setBiometric(bio); setHasOnboarded(true); router.replace(routes.home); }; const sendPhoneCode = () => { if (!name.trim() || phone.replace(/\D/g, "").length < 9) { Alert.alert("Missing info", "Enter your name and a valid phone number."); return; } setSent(true); Alert.alert( "Code sent (demo)", `Use OTP ${DEMO_OTP} to verify. No real SMS is sent in this build.` ); }; const verifyPhone = async () => { if (!sent) return; if (code.trim() !== DEMO_OTP) { Alert.alert("Invalid OTP", `Enter the demo code ${DEMO_OTP}.`); return; } await finishSignIn({ name, phone, email: email.trim() || undefined, }); }; const signInEmail = async () => { if (!name.trim()) { Alert.alert("Missing info", "Enter your name."); return; } if (!email.includes("@")) { Alert.alert("Invalid email", "Enter a valid email address."); return; } if (password.trim() !== DEMO_EMAIL_PASSWORD) { Alert.alert( "Wrong password", `Demo password is ${DEMO_EMAIL_PASSWORD}.` ); return; } await finishSignIn({ name, phone: phone.trim() || "+251911000000", email, }); }; const signInSocial = async (provider: "google" | "telegram") => { if (!name.trim()) { Alert.alert("Missing info", "Enter the name to show on your profile."); return; } if (provider === "telegram" && !telegram.trim()) { Alert.alert("Telegram", "Enter your Telegram username."); return; } setBusy(true); // Simulated OAuth / Telegram Login Widget handshake await new Promise((r) => setTimeout(r, 900)); setBusy(false); const resolvedEmail = provider === "google" ? email.trim() || `${name.split(" ")[0]?.toLowerCase() || "user"}@gmail.com` : email.trim() || undefined; const resolvedPhone = phone.trim() || (provider === "telegram" ? "+251900000000" : "+251911000000"); await finishSignIn({ name: name.trim(), phone: resolvedPhone, email: resolvedEmail, }); }; return ( {busy ? ( Connecting to {method === "google" ? "Google" : "Telegram"}… ) : ( <> {method === "phone" ? ( <> {sent ? ( ) : null} ) : null} {method === "email" ? ( <> ) : null} {method === "google" ? ( <> ) : null} {method === "telegram" ? ( <> ) : null} {method === "phone" ? ( !sent ? (