import React, { useState } from 'react';
import { View, Text, Image, StyleSheet, TouchableOpacity, ScrollView, Alert } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { ZitadelAuth } from '@zitadel/react';

const AuthenticationScreen = () => {
  const [isAuthenticated, setIsAuthenticated] = useState(false);

  const handleLogin = async () => {
    try {
      const auth = new ZitadelAuth({
        clientId: 'YOUR_CLIENT_ID',
        issuer: 'YOUR_ZITADEL_ISSUER_URL',
        redirectUri: 'YOUR_REDIRECT_URI',
        scopes: ['openid', 'profile', 'email'],
      });

      const result = await auth.authorize();
      if (result?.accessToken) {
        await AsyncStorage.setItem('authToken', result.accessToken);
        setIsAuthenticated(true);
        Alert.alert('Login Successful', 'You are now authenticated.');
      } else {
        Alert.alert('Login Failed', 'Unable to retrieve access token.');
      }
    } catch (error) {
      console.error('Login error:', error);
      Alert.alert('Login Error', 'An error occurred during login.');
    }
  };

  const handleLogout = async () => {
    try {
      await AsyncStorage.removeItem('authToken');
      setIsAuthenticated(false);
      Alert.alert('Logout Successful', 'You are now logged out.');
    } catch (error) {
      console.error('Logout error:', error);
      Alert.alert('Logout Error', 'An error occurred during logout.');
    }
  };

  return (
    <SafeAreaView style={styles.container}>
      <ScrollView contentContainerStyle={styles.scrollContent}>
        <View style={styles.imageContainer}>
        </View>

        <View style={styles.contentContainer}>
          <TouchableOpacity style={styles.loginButton} onPress={isAuthenticated ? handleLogout : handleLogin}>
            <Text style={styles.loginButtonText}>{isAuthenticated ? 'Logout' : 'Login'}</Text>
          </TouchableOpacity>

          <View style={styles.leftPanel}>
            <View style={styles.logoContainer}>
              {/* Replace with your logo component */}
              <Text style={styles.logoText}>Welcome to General Bots Online</Text>
            </View>
            <View style={styles.quoteContainer}>
              <Text style={styles.quoteText}>
                "Errar é Humano."
              </Text>
              <Text style={styles.quoteAuthor}>General Bots</Text>
            </View>
          </View>

          <View style={styles.formContainer}>
            <View style={styles.formHeader}>
              <Text style={styles.formTitle}>Create an account</Text>
              <Text style={styles.formSubtitle}>
                Enter your email below to create your account
              </Text>
            </View>
          
            <Text style={styles.termsText}>
              By clicking continue, you agree to our Terms of Service and Privacy Policy.
            </Text>
          </View>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
  },
  scrollContent: {
    flexGrow: 1,
  },
  imageContainer: {
    // ... styles for image container
  },
  image: {
    width: '100%',
    height: 300,
    resizeMode: 'cover',
  },
  contentContainer: {
    flex: 1,
    padding: 16,
  },
  loginButton: {
    position: 'absolute',
    top: 16,
    right: 16,
    padding: 8,
  },
  loginButtonText: {
    color: '#000',
  },
  leftPanel: {
    // ... styles for left panel
  },
  logoContainer: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  logoText: {
    fontSize: 18,
    fontWeight: 'bold',
  },
  quoteContainer: {
    marginTop: 'auto',
  },
  quoteText: {
    fontSize: 16,
    marginBottom: 8,
  },
  quoteAuthor: {
    fontSize: 14,
  },
  formContainer: {
    // ... styles for form container
  },
  formHeader: {
    alignItems: 'center',
    marginBottom: 16,
  },
  formTitle: {
    fontSize: 24,
    fontWeight: 'bold',
  },
  formSubtitle: {
    fontSize: 14,
    color: '#666',
  },
  termsText: {
    textAlign: 'center',
    fontSize: 12,
    color: '#666',
    marginTop: 16,
  },
});

export default AuthenticationScreen;