import { Redirect, router } from 'expo-router';
import { ActivityIndicator, StyleSheet, Text, View } from 'react-native';

import { Button } from '@/components/common/Button';
import { Palette } from '@/constants/palette';
import { Radius, Spacing } from '@/constants/styles';
import { useAuth } from '@/providers/AuthProvider';
import { logout } from '@/services/auth';

export default function Home() {
  const { initializing, user } = useAuth();

  if (initializing) {
    return (
      <View style={styles.centered}>
        <ActivityIndicator color={Palette.black} />
      </View>
    );
  }

  if (!user) {
    return <Redirect href="/login" />;
  }

  return (
    <View style={styles.container}>
      <Text style={styles.eyebrow}>Session active</Text>
      <Text style={styles.title}>Bienvenue</Text>
      <Text style={styles.subtitle}>
        {user.displayName || user.email || user.uid}
      </Text>

      <View style={styles.card}>
        <Text style={styles.label}>UID</Text>
        <Text style={styles.value}>{user.uid}</Text>
        <Text style={styles.label}>Email</Text>
        <Text style={styles.value}>{user.email || 'Aucun email'}</Text>
      </View>

      <Button onPress={() => void logout()} title="Se deconnecter" />
      <Button
        onPress={() => router.push('/register')}
        style={styles.secondaryButton}
        textStyle={styles.secondaryButtonText}
        title="Creer un autre compte"
        variant="secondary"
      />
    </View>
  );
}

const styles = StyleSheet.create({
  centered: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: Palette.cream,
  },
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingHorizontal: Spacing.lg,
    gap: 20,
    backgroundColor: Palette.cream,
  },
  eyebrow: {
    color: Palette.gray,
    fontSize: 13,
    fontWeight: '700',
    letterSpacing: 1.1,
    textTransform: 'uppercase',
  },
  title: {
    color: Palette.black,
    fontSize: 34,
    fontWeight: '800',
  },
  subtitle: {
    color: Palette.gray,
    fontSize: 16,
    lineHeight: 22,
  },
  card: {
    gap: Spacing.sm,
    padding: 18,
    borderRadius: Radius.xl,
    borderWidth: 1,
    borderColor: Palette.cream,
    backgroundColor: Palette.white,
  },
  label: {
    color: Palette.gray,
    fontSize: 12,
    fontWeight: '700',
    textTransform: 'uppercase',
  },
  value: {
    color: Palette.black,
    fontSize: 15,
  },
  secondaryButton: {
    borderColor: Palette.black,
  },
  secondaryButtonText: {
    color: Palette.black,
  },
});
