import { ThemedText } from "@/components/themed-text";
import { ThemedTextInput } from "@/components/themed-textinput";
import { ThemedView } from "@/components/themed-view";
import { router } from "expo-router";
import {
  signInWithEmailAndPassword
} from "firebase/auth";
import React, { useState } from "react";
import { Button, StyleSheet, View } from "react-native";
import { auth } from "../../firebase_config";

const DEFAULT_ROLE = "resp";

const LoginScreen: React.FC = () => {
  const [email, setEmail] = useState<string>("");
  const [password, setPassword] = useState<string>("");

  const handleLogin = async () => {
    try {
      await signInWithEmailAndPassword(auth, email, password);
      router.replace("/(tabs)");
    } catch (error: any) {
      alert(error.message);
    }
  };

  /*const handleRegister = async () => {
    try {
      await createUserWithEmailAndPassword(auth, email, password);
      router.replace("/(tabs)");
    } catch (error: any) {
      alert(error.message);
    }
  }; */

  return (
    <ThemedView lvl={1} style={styles.container}>
      <ThemedText style={styles.title}>Se connecter / S'incrire</ThemedText>
      <ThemedTextInput
        lvl = {2}
        border = {5}
        style={[styles.input]}
        placeholder="Email:"
        placeholderTextColor={'white'}
        value={email}
        onChangeText={setEmail}
        autoCapitalize="none"

      />
      <ThemedTextInput
        lvl = {2}
        border = {5}
        style={[styles.input]}
        placeholder="Mot de passe:"
        placeholderTextColor={'white'}
        value={password}
        onChangeText={setPassword}
        secureTextEntry
      />
      <Button title="Se connecter" onPress={handleLogin} />
      <View style={{ height: 10 }} />
      {/* <Button title="S'inscrire" onPress={handleRegister} /> */}
    </ThemedView>
  );
};

export default LoginScreen;

const styles = StyleSheet.create({
  container: { flex: 1, padding: 20, paddingTop: 100},
  title: {
    fontSize: 22,
    fontWeight: "bold",
    marginBottom: 20,
    textAlign: "center",
  },
  input: {
    borderWidth: 1,
    //borderColor: "#ccc",
    borderRadius: 8,
    padding: 10,
    marginBottom: 10,
  },
});
