import { Controller, useForm } from "react-hook-form";
import {
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from "react-native";
import { CustomTextInput } from "../text_input/text_input";
import { LIGTHT_THEME } from "../../constants/theme";
import { useLoggin } from "../../hooks/useLoggin";
import { OrDivision } from "../or_division/or_division";
import { useSignUp } from "../../hooks/useSignUp";
import { DateTextInput } from "../date_text_input/date_text_input";

export const SignUpForm = () => {
  const { control, onSubmit } = useSignUp();
  return (
    <ScrollView contentContainerStyle={styles.loginScroll}>
      <Text style={styles.title}>Sign Up</Text>
      <View style={{ height: 40 }} />
      <Controller
        name="name"
        control={control}
        defaultValue=""
        render={({
          field: { onChange, onBlur, value },
          formState: { errors },
        }) => (
          <CustomTextInput
            label="Name"
            value={value}
            onChangeText={onChange}
            onBlur={onBlur}
            errors={errors.name?.message as string}
          />
        )}
        rules={{
          required: "Name is required",
        }}
      />
      <Controller
        name="lastName"
        control={control}
        defaultValue=""
        render={({
          field: { onChange, onBlur, value },
          formState: { errors },
        }) => (
          <CustomTextInput
            label="Last Name"
            value={value}
            onChangeText={onChange}
            onBlur={onBlur}
            errors={errors.lastName?.message as string}
          />
        )}
        rules={{
          required: "Last name is required",
        }}
      />
      <Controller
        name="email"
        control={control}
        defaultValue=""
        render={({
          field: { onChange, onBlur, value },
          formState: { errors },
        }) => (
          <CustomTextInput
            label="Email"
            value={value}
            onChangeText={onChange}
            onBlur={onBlur}
            errors={errors.email?.message as string}
          />
        )}
        rules={{
          required: "Email is required",
          pattern: { value: /\S+@\S+\.\S+/, message: "Invalid email" },
        }}
      />
      <Controller
        name="password"
        control={control}
        defaultValue=""
        render={({
          field: { onChange, onBlur, value },
          formState: { errors },
        }) => (
          <CustomTextInput
            label="Password"
            isPassword
            value={value}
            onChangeText={onChange}
            onBlur={onBlur}
            errors={errors.password?.message as string}
          />
        )}
        rules={{ required: "Password is required" }}
      />
      <Controller
        name="confirmPassword"
        control={control}
        defaultValue=""
        render={({
          field: { onChange, onBlur, value },
          formState: { errors },
        }) => (
          <CustomTextInput
            label="Confirm Password"
            isPassword
            value={value}
            onChangeText={onChange}
            onBlur={onBlur}
            errors={errors.confirmPassword?.message as string}
          />
        )}
        rules={{ required: "Confirm password is required" }}
      />
      <Controller
        control={control}
        name="dateOfBirth"
        render={({ field: { onChange, value } }) => (
          <DateTextInput
            label="Date of Birth"
            value={value}
            onChangeText={onChange}
          />
        )}
        defaultValue=" "

      />
      <View style={{ height: "3%" }} />
      <TouchableOpacity style={styles.submitBtn} onPress={onSubmit}>
        <Text style={styles.submitBtnText}>Sign Up</Text>
      </TouchableOpacity>

      <OrDivision/>
    </ScrollView>
  );
};

const styles = StyleSheet.create({
  mainContainer: {
    flex: 1,
    backgroundColor: LIGTHT_THEME.color.background,
  },
  imageContainer: {
    height: "40%",
    overflow: "hidden",
    borderBottomLeftRadius: 70,
    elevation: 5,
  },
  loginContainer: {
    marginHorizontal: 20,
    marginTop: "-30%",
    marginBottom: 20,
    elevation: 5,
    borderRadius: 10,
    backgroundColor: LIGTHT_THEME.color.white,
    height: "70%",
  },
  loginScroll: {
    padding: 20,
    gap: 10,
    justifyContent: "center",
  },
  title: { fontSize: 30, fontWeight: "bold", alignSelf: "center" },
  submitBtn: {
    backgroundColor: "black",
    borderRadius: 5,
    width: "100%",
    padding: 10,
    justifyContent: "center",
    alignItems: "center",
  },
  submitBtnText: {
    color: "white",
    fontSize: 20,
  },
});
