import React, { useState } from 'react';
import { View, Text, TextInput, TouchableOpacity, ActivityIndicator, Alert, SafeAreaView, KeyboardAvoidingView, Platform } from 'react-native';
import { useAuth } from '../context/AuthContext';

export const LoginScreen: React.FC = () => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [isSubmitting, setIsSubmitting] = useState(false);
  const { login } = useAuth();

  const handleLogin = async () => {
    if (!email || !password) {
      Alert.alert('Error', 'Silakan masukkan email dan password.');
      return;
    }
    
    setIsSubmitting(true);
    try {
      await login(email, password);
    } catch (e: any) {
      const msg = e.response?.data?.message || 'Login gagal. Periksa kembali email dan password Anda.';
      Alert.alert('Gagal Masuk', msg);
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <SafeAreaView className="flex-1 bg-slate-body justify-center">
      <KeyboardAvoidingView
        behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
        className="px-6"
      >
        <View className="bg-white rounded-3xl p-8 shadow-xl border border-slate-light/10">
          {/* Header Branding */}
          <View className="items-center mb-8">
            <View className="w-16 h-16 bg-primary rounded-2xl items-center justify-center shadow-lg shadow-primary-glow mb-4">
              <Text className="text-white text-3xl font-extrabold">PK</Text>
            </View>
            <Text className="text-2xl font-extrabold text-slate-main tracking-tight">
              POS Kasir
            </Text>
            <Text className="text-xs text-slate-sub font-semibold mt-1">
              Aplikasi Kasir Online Premium
            </Text>
          </View>

          {/* Form Inputs */}
          <View className="space-y-4">
            <View>
              <Text className="text-xs font-bold text-slate-sub mb-2">EMAIL ALAMAT</Text>
              <TextInput
                className="bg-slate-body border border-slate-light/20 rounded-xl px-4 py-3 text-slate-main font-semibold"
                placeholder="email@toko.com"
                placeholderTextColor="#94A3B8"
                keyboardType="email-address"
                autoCapitalize="none"
                value={email}
                onChangeText={setEmail}
              />
            </View>

            <View className="mt-4">
              <Text className="text-xs font-bold text-slate-sub mb-2">PASSWORD</Text>
              <TextInput
                className="bg-slate-body border border-slate-light/20 rounded-xl px-4 py-3 text-slate-main font-semibold"
                placeholder="Masukkan password Anda"
                placeholderTextColor="#94A3B8"
                secureTextEntry
                autoCapitalize="none"
                value={password}
                onChangeText={setPassword}
              />
            </View>
          </View>

          {/* Submit Button */}
          <TouchableOpacity
            className="bg-primary rounded-xl py-4 mt-8 items-center justify-center shadow-lg"
            onPress={handleLogin}
            disabled={isSubmitting}
          >
            {isSubmitting ? (
              <ActivityIndicator color="#ffffff" />
            ) : (
              <Text className="text-white font-extrabold text-base">MASUK KE KASIR</Text>
            )}
          </TouchableOpacity>

          {/* Footer Info */}
          <View className="mt-8 items-center">
            <Text className="text-slate-light text-[10px] font-medium">
              Dilindungi enkripsi aman &bull; poskasir.my.id
            </Text>
          </View>
        </View>
      </KeyboardAvoidingView>
    </SafeAreaView>
  );
};
