import React, { useState } from 'react';
import {
  View,
  Text,
  TextInput,
  TouchableOpacity,
  ScrollView,
} from 'react-native';
import { useRouter, useLocalSearchParams } from 'expo-router';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useLanguage } from '@/i18n/LanguageContext';
import { useTheme } from '@/constants/ThemeContext';
import { bookingStyles } from '@/styles/booking.styles';
import { ArrowLeft, User, Phone, CreditCard, Luggage } from 'lucide-react-native';

export default function PassengerBookingScreen() {
  const router = useRouter();
  const params = useLocalSearchParams();
  const { t } = useLanguage();
  const { isDark } = useTheme();

  const tripId = (params.tripId as string) || '';
  const routeName = (params.route as string) || 'Bamenda to Buea';
  const agencyName = (params.agencyName as string) || 'Garanti Express';
  const departureTime = (params.departureTime as string) || 'Today 07:30am';
  const selectedSeats = (params.selectedSeats as string) || '';
  const selectedSeatIds = (params.selectedSeatIds as string) || '';
  const totalPrice = (params.totalPrice as string) || '0';

  const [fullName, setFullName] = useState('Jean Perrin');
  const [phoneNumber, setPhoneNumber] = useState('+237 670 000 000');
  const [idCardNumber, setIdCardNumber] = useState('CNI-102938475');
  const [luggageCount, setLuggageCount] = useState('1 Bag');

  const handleProceedToPayment = () => {
    if (!selectedSeats) {
      return;
    }
    router.push({
      pathname: '/booking/payment',
      params: {
        tripId,
        route: routeName,
        agencyName,
        departureTime,
        selectedSeats,
        selectedSeatIds,
        totalPrice,
        fullName,
        phoneNumber,
        idCardNumber,
      },
    } as any);
  };

  return (
    <SafeAreaView
      edges={['top', 'left', 'right']}
      style={[
        bookingStyles.container,
        { backgroundColor: isDark ? '#071126' : '#F8FAFC' },
      ]}
    >
      <ScrollView
        showsVerticalScrollIndicator={false}
        contentContainerStyle={bookingStyles.scrollContent}
      >
        <TouchableOpacity
          onPress={() => router.back()}
          style={{ flexDirection: 'row', alignItems: 'center', marginBottom: 12 }}
        >
          <ArrowLeft size={20} color={isDark ? '#F8FAFC' : '#0B1F4D'} />
          <Text style={{ fontSize: 16, fontWeight: '800', color: isDark ? '#F8FAFC' : '#0B1F4D', marginLeft: 8 }}>
            Back to Seat Map
          </Text>
        </TouchableOpacity>

        <Text style={isDark ? bookingStyles.headerTitleDark : bookingStyles.headerTitleLight}>
          Passenger Details
        </Text>
        <Text style={isDark ? bookingStyles.headerSubtitleDark : bookingStyles.headerSubtitleLight}>
          Confirm passenger details for your e-ticket & boarding pass
        </Text>

        {/* Trip Summary Card */}
        <View
          style={[
            bookingStyles.summaryCard,
            isDark ? bookingStyles.summaryCardDark : bookingStyles.summaryCardLight,
          ]}
        >
          <Text style={isDark ? bookingStyles.cardTitleDark : bookingStyles.cardTitleLight}>
            Trip Summary
          </Text>
          <View style={bookingStyles.rowItem}>
            <Text style={isDark ? bookingStyles.labelDark : bookingStyles.labelLight}>Route:</Text>
            <Text style={isDark ? bookingStyles.valueDark : bookingStyles.valueLight}>{routeName}</Text>
          </View>
          <View style={bookingStyles.rowItem}>
            <Text style={isDark ? bookingStyles.labelDark : bookingStyles.labelLight}>Agency:</Text>
            <Text style={isDark ? bookingStyles.valueDark : bookingStyles.valueLight}>{agencyName}</Text>
          </View>
          <View style={bookingStyles.rowItem}>
            <Text style={isDark ? bookingStyles.labelDark : bookingStyles.labelLight}>Seat(s):</Text>
            <Text style={{ fontSize: 13, fontWeight: '900', color: selectedSeats ? '#15803D' : '#94A3B8' }}>
              {selectedSeats || 'No seat selected'}
            </Text>
          </View>
          <View style={bookingStyles.rowItem}>
            <Text style={isDark ? bookingStyles.labelDark : bookingStyles.labelLight}>Total Fare:</Text>
            <Text style={{ fontSize: 15, fontWeight: '900', color: isDark ? '#38BDF8' : '#0B1F4D' }}>
              {totalPrice} FCFA
            </Text>
          </View>
        </View>

        {/* Input Fields */}
        <View style={bookingStyles.inputGroup}>
          <Text style={isDark ? bookingStyles.fieldLabelDark : bookingStyles.fieldLabelLight}>
            Full Passenger Name
          </Text>
          <TextInput
            value={fullName}
            onChangeText={setFullName}
            placeholder="Enter full name"
            placeholderTextColor="#94A3B8"
            style={[
              bookingStyles.textInput,
              isDark ? bookingStyles.textInputDark : bookingStyles.textInputLight,
            ]}
          />
        </View>

        <View style={bookingStyles.inputGroup}>
          <Text style={isDark ? bookingStyles.fieldLabelDark : bookingStyles.fieldLabelLight}>
            Phone Number (for SMS & Mobile Money)
          </Text>
          <TextInput
            value={phoneNumber}
            onChangeText={setPhoneNumber}
            placeholder="+237 6XX XXX XXX"
            keyboardType="phone-pad"
            placeholderTextColor="#94A3B8"
            style={[
              bookingStyles.textInput,
              isDark ? bookingStyles.textInputDark : bookingStyles.textInputLight,
            ]}
          />
        </View>

        <View style={bookingStyles.inputGroup}>
          <Text style={isDark ? bookingStyles.fieldLabelDark : bookingStyles.fieldLabelLight}>
            National ID Card / Passport Number
          </Text>
          <TextInput
            value={idCardNumber}
            onChangeText={setIdCardNumber}
            placeholder="CNI Number"
            placeholderTextColor="#94A3B8"
            style={[
              bookingStyles.textInput,
              isDark ? bookingStyles.textInputDark : bookingStyles.textInputLight,
            ]}
          />
        </View>

        <View style={bookingStyles.inputGroup}>
          <Text style={isDark ? bookingStyles.fieldLabelDark : bookingStyles.fieldLabelLight}>
            Luggage Count
          </Text>
          <TextInput
            value={luggageCount}
            onChangeText={setLuggageCount}
            placeholder="e.g. 1 Bag"
            placeholderTextColor="#94A3B8"
            style={[
              bookingStyles.textInput,
              isDark ? bookingStyles.textInputDark : bookingStyles.textInputLight,
            ]}
          />
        </View>

        <TouchableOpacity
          onPress={handleProceedToPayment}
          style={bookingStyles.primaryBtn}
        >
          <Text style={bookingStyles.primaryBtnText}>Proceed to Payment</Text>
        </TouchableOpacity>
      </ScrollView>
    </SafeAreaView>
  );
}
