import { Ionicons } from "@expo/vector-icons";
import { router } from "expo-router";
import { useState } from "react";
import {
  KeyboardAvoidingView,
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { useAuth } from "../../lib/auth";
import { t } from "../../lib/locales";
import { colors } from "../../lib/theme";
import { Button, H1, Muted, TextInput } from "../../lib/ui";

export default function SignUp() {
  const { signUp } = useAuth();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [confirmPassword, setConfirmPassword] = useState("");
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [emailError, setEmailError] = useState("");
  const [passwordError, setPasswordError] = useState("");
  const [confirmPasswordError, setConfirmPasswordError] = useState("");

  const validateForm = () => {
    let valid = true;
    setEmailError("");
    setPasswordError("");
    setConfirmPasswordError("");

    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(email)) {
      setEmailError(t("invalidEmail"));
      valid = false;
    }

    if (!password) {
      setPasswordError(t("passwordRequired"));
      valid = false;
    } else if (password.length < 8) {
      setPasswordError(t("passwordTooShort"));
      valid = false;
    }

    if (password !== confirmPassword) {
      setConfirmPasswordError(t("passwordsDoNotMatch"));
      valid = false;
    }

    return valid;
  };

  const handleSignUp = async () => {
    if (!validateForm()) return;

    setLoading(true);
    setError("");

    try {
      const result = await signUp(email, password);
      if (!result.success) {
        setError(result.reason || t("signUpError"));
      }
    } catch (e) {
      setError(t("signUpError"));
    } finally {
      setLoading(false);
    }
  };

  return (
    <SafeAreaView style={styles.container}>
      <KeyboardAvoidingView
        style={styles.keyboardView}
        behavior={Platform.OS === "ios" ? "padding" : "height"}
      >
        <ScrollView
          contentContainerStyle={styles.scrollContent}
          keyboardShouldPersistTaps="handled"
        >
          <TouchableOpacity
            style={styles.backButton}
            onPress={() => router.back()}
          >
            <Ionicons name="arrow-back" size={24} color={colors.onBackground} />
          </TouchableOpacity>

          <View style={styles.header}>
            <H1>{t("createAccount")}</H1>
          </View>

          <View style={styles.form}>
            <TextInput
              label={t("email")}
              value={email}
              onChangeText={setEmail}
              placeholder={t("emailPlaceholder")}
              keyboardType="email-address"
              autoCapitalize="none"
              error={emailError}
            />

            <TextInput
              label={t("password")}
              value={password}
              onChangeText={setPassword}
              placeholder={t("passwordPlaceholder")}
              secureTextEntry
              error={passwordError}
            />

            <TextInput
              label={t("confirmPassword")}
              value={confirmPassword}
              onChangeText={setConfirmPassword}
              placeholder={t("confirmPasswordPlaceholder")}
              secureTextEntry
              error={confirmPasswordError}
            />

            {error ? <Text style={styles.error}>{error}</Text> : null}

            <Button
              title={t("signUp")}
              onPress={handleSignUp}
              loading={loading}
              disabled={loading}
            />
          </View>

          <View style={styles.footer}>
            <Muted>{t("alreadyHaveAccount")}</Muted>
            <TouchableOpacity onPress={() => router.replace("/(auth)/signin")}>
              <Text style={styles.link}>{t("signIn")}</Text>
            </TouchableOpacity>
          </View>
        </ScrollView>
      </KeyboardAvoidingView>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: colors.background,
  },
  keyboardView: {
    flex: 1,
  },
  scrollContent: {
    flexGrow: 1,
    padding: 24,
  },
  backButton: {
    marginBottom: 16,
  },
  header: {
    marginBottom: 32,
  },
  form: {
    gap: 20,
  },
  error: {
    color: colors.primary,
    fontSize: 14,
    textAlign: "center",
  },
  footer: {
    flexDirection: "row",
    justifyContent: "center",
    alignItems: "center",
    gap: 8,
    marginTop: 32,
  },
  link: {
    color: colors.primary,
    fontWeight: "700",
  },
});
