import React from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  ScrollView,
  Image,
} from 'react-native';
import { useRouter, useLocalSearchParams } from 'expo-router';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useTheme } from '@/constants/ThemeContext';
import { bookingStyles } from '@/styles/booking.styles';
import { CheckCircle2, QrCode, Home, Ticket } from 'lucide-react-native';

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

  const routeName = (params.route as string) || 'Bamenda to Buea';
  const agencyName = (params.agencyName as string) || 'Garanti Express';
  const selectedSeats = (params.selectedSeats as string) || '';
  const totalPrice = (params.totalPrice as string) || '0';
  const fullName = (params.fullName as string) || 'Jean Perrin';
  const bookingRef = (params.bookingRef as string) || 'TC-CONF-88492';
  const ticketNumber = (params.ticketNumber as string) || '';

  return (
    <SafeAreaView
      edges={['top', 'left', 'right']}
      style={[
        bookingStyles.container,
        { backgroundColor: isDark ? '#071126' : '#F8FAFC' },
      ]}
    >
      <ScrollView
        showsVerticalScrollIndicator={false}
        contentContainerStyle={[bookingStyles.scrollContent, { alignItems: 'center', paddingTop: 30 }]}
      >
        <View style={{ width: 68, height: 68, borderRadius: 34, backgroundColor: '#DCFCE7', justifyContent: 'center', alignItems: 'center', marginBottom: 14 }}>
          <CheckCircle2 size={44} color="#15803D" />
        </View>

        <Text style={[isDark ? bookingStyles.headerTitleDark : bookingStyles.headerTitleLight, { textAlign: 'center' }]}>
          Payment Successful!
        </Text>
        <Text style={[isDark ? bookingStyles.headerSubtitleDark : bookingStyles.headerSubtitleLight, { textAlign: 'center', marginBottom: 20 }]}>
          Your bus reservation has been validated & e-ticket issued.
        </Text>

        {/* E-Ticket Card */}
        <View
          style={[
            bookingStyles.summaryCard,
            isDark ? bookingStyles.summaryCardDark : bookingStyles.summaryCardLight,
            { width: '100%', padding: 20, alignItems: 'center', borderColor: isDark ? '#38BDF8' : '#CBD5E1' },
          ]}
        >
          <View style={{ backgroundColor: '#0B1F4D', paddingHorizontal: 12, paddingVertical: 4, borderRadius: 8, marginBottom: 12, alignItems: 'center' }}>
            <Text style={{ color: '#FFFFFF', fontWeight: '900', fontSize: 12 }}>
              REF: {bookingRef}
            </Text>
            {ticketNumber ? (
              <Text style={{ color: '#38BDF8', fontWeight: '700', fontSize: 11, marginTop: 2 }}>
                TICKET: {ticketNumber}
              </Text>
            ) : null}
          </View>

          <Text style={{ fontSize: 18, fontWeight: '900', color: isDark ? '#FFFFFF' : '#0B1F4D', marginBottom: 4 }}>
            {routeName}
          </Text>
          <Text style={{ fontSize: 13, fontWeight: '700', color: isDark ? '#38BDF8' : '#0284C7', marginBottom: 12 }}>
            {agencyName} • {selectedSeats ? `Seat ${selectedSeats}` : 'No seat assigned'}
          </Text>

          <View style={{ width: '100%', height: 1, backgroundColor: isDark ? '#1E293B' : '#E2E8F0', marginVertical: 10 }} />

          <View style={{ width: '100%', marginBottom: 16 }}>
            <View style={bookingStyles.rowItem}>
              <Text style={isDark ? bookingStyles.labelDark : bookingStyles.labelLight}>Passenger:</Text>
              <Text style={isDark ? bookingStyles.valueDark : bookingStyles.valueLight}>{fullName}</Text>
            </View>
            <View style={bookingStyles.rowItem}>
              <Text style={isDark ? bookingStyles.labelDark : bookingStyles.labelLight}>Amount Paid:</Text>
              <Text style={{ fontSize: 14, fontWeight: '900', color: '#15803D' }}>{totalPrice} FCFA</Text>
            </View>
            <View style={bookingStyles.rowItem}>
              <Text style={isDark ? bookingStyles.labelDark : bookingStyles.labelLight}>Status:</Text>
              <Text style={{ fontSize: 13, fontWeight: '900', color: '#15803D' }}>PAID & CONFIRMED</Text>
            </View>
          </View>

          {/* QR Code Graphic Placeholder */}
          <View style={{ backgroundColor: '#FFFFFF', padding: 12, borderRadius: 12, borderWidth: 1, borderColor: '#CBD5E1', alignItems: 'center' }}>
            <QrCode size={110} color="#0F172A" />
            <Text style={{ fontSize: 10, fontWeight: '800', color: '#64748B', marginTop: 4 }}>
              Scan at terminal boarding gate
            </Text>
          </View>
        </View>

        <TouchableOpacity
          onPress={() => router.replace('/(tabs)' as any)}
          style={[bookingStyles.primaryBtn, { width: '100%', marginTop: 10 }]}
        >
          <Text style={bookingStyles.primaryBtnText}>Return to Home Dashboard</Text>
        </TouchableOpacity>
      </ScrollView>
    </SafeAreaView>
  );
}
