import React from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  ScrollView,
  Share,
} 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 { ticketsStyles, ticketsTheme } from '@/styles/tickets.styles';
import { ArrowLeft, Share2, QrCode } from 'lucide-react-native';

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

  const ticketId = (params.ticketId as string) || 'TC-78291';
  const route = (params.route as string) || 'Bamenda to Buea';
  const agencyName = (params.agencyName as string) || 'Garanti Express';
  const busNumber = (params.busNumber as string) || 'VIP-104';
  const seatNumber = (params.seatNumber as string) || 'B05';
  const departureDate = (params.departureDate as string) || 'Today';
  const departureTime = (params.departureTime as string) || 'Today 08:30 am';
  const arrivalTime = (params.arrivalTime as string) || '02:30 pm';
  const totalPrice = (params.totalPrice as string) || '6,500 FCFA';
  const passengerName = (params.passengerName as string) || 'Passenger';
  const status = (params.status as string) || 'Active';

  const handleShare = async () => {
    try {
      await Share.share({
        message: `TravelCam E-Ticket (${ticketId})\nPassenger: ${passengerName}\nRoute: ${route}\nAgency: ${agencyName}\nSeat: ${seatNumber}\nDeparture: ${departureTime}\nPrice: ${totalPrice}`,
      });
    } catch (e) {
      console.log(e);
    }
  };

  return (
    <SafeAreaView
      edges={['top', 'left', 'right']}
      style={[
        ticketsStyles.container,
        { backgroundColor: isDark ? ticketsTheme.colors.navyBg : ticketsTheme.colors.lightBg },
      ]}
    >
      <ScrollView showsVerticalScrollIndicator={false} contentContainerStyle={{ paddingBottom: 40 }}>
        {/* Header Bar */}
        <View
          style={{
            flexDirection: 'row',
            alignItems: 'center',
            justifyContent: 'space-between',
            paddingHorizontal: 18,
            paddingVertical: 14,
          }}
        >
          <TouchableOpacity onPress={() => router.back()} style={{ padding: 4 }}>
            <ArrowLeft size={22} color={isDark ? '#F8FAFC' : '#0B1F4D'} />
          </TouchableOpacity>
          <Text
            style={{
              fontSize: 18,
              fontWeight: '900',
              color: isDark ? '#F8FAFC' : '#0B1F4D',
            }}
          >
            Reservation Details
          </Text>
          <TouchableOpacity onPress={handleShare} style={{ padding: 4 }}>
            <Share2 size={20} color="#E11D48" />
          </TouchableOpacity>
        </View>

        {/* Detail Ticket Card */}
        <View
          style={[
            ticketsStyles.detailCard,
            isDark && ticketsStyles.detailCardDark,
          ]}
        >
          {/* Header Route & Date */}
          <Text style={isDark ? ticketsStyles.detailRouteTextDark : ticketsStyles.detailRouteTextLight}>
            {route}
          </Text>
          <Text style={ticketsStyles.detailDateText}>{departureDate}</Text>

          <View style={ticketsStyles.dashedDivider} />

          {/* Grid Row 1: Passenger & Seat Number */}
          <View style={ticketsStyles.detailGridRow}>
            <View style={ticketsStyles.detailGridItem}>
              <Text style={ticketsStyles.detailLabel}>Passenger</Text>
              <Text style={isDark ? ticketsStyles.detailValueDark : ticketsStyles.detailValueLight}>
                {passengerName}
              </Text>
            </View>
            <View style={ticketsStyles.detailGridItem}>
              <Text style={ticketsStyles.detailLabel}>Seat Number</Text>
              <Text style={isDark ? ticketsStyles.detailValueDark : ticketsStyles.detailValueLight}>
                Seat {seatNumber}
              </Text>
            </View>
          </View>

          {/* Grid Row 2: Agency Name & Bus Number */}
          <View style={ticketsStyles.detailGridRow}>
            <View style={ticketsStyles.detailGridItem}>
              <Text style={ticketsStyles.detailLabel}>Agency</Text>
              <Text style={isDark ? ticketsStyles.detailValueDark : ticketsStyles.detailValueLight}>
                {agencyName}
              </Text>
            </View>
            <View style={ticketsStyles.detailGridItem}>
              <Text style={ticketsStyles.detailLabel}>Bus Plate</Text>
              <Text style={isDark ? ticketsStyles.detailValueDark : ticketsStyles.detailValueLight}>
                {busNumber}
              </Text>
            </View>
          </View>

          {/* Grid Row 3: Ticket ID & Status */}
          <View style={ticketsStyles.detailGridRow}>
            <View style={ticketsStyles.detailGridItem}>
              <Text style={ticketsStyles.detailLabel}>Ticket ID</Text>
              <Text style={[isDark ? ticketsStyles.detailValueDark : ticketsStyles.detailValueLight, { fontSize: 13 }]}>
                {ticketId}
              </Text>
            </View>
            <View style={ticketsStyles.detailGridItem}>
              <Text style={ticketsStyles.detailLabel}>Status</Text>
              <Text style={{ fontSize: 13, fontWeight: '900', color: status === 'Active' ? '#15803D' : status === 'Completed' ? '#0284C7' : '#DC2626' }}>
                ● {status.toUpperCase()}
              </Text>
            </View>
          </View>

          {/* Grid Row 4: Departure & Arrival Time */}
          <View style={ticketsStyles.detailGridRow}>
            <View style={ticketsStyles.detailGridItem}>
              <Text style={ticketsStyles.detailLabel}>Departure</Text>
              <Text style={isDark ? ticketsStyles.detailValueDark : ticketsStyles.detailValueLight}>
                {departureTime}
              </Text>
            </View>
            <View style={ticketsStyles.detailGridItem}>
              <Text style={ticketsStyles.detailLabel}>Arrival</Text>
              <Text style={isDark ? ticketsStyles.detailValueDark : ticketsStyles.detailValueLight}>
                {arrivalTime}
              </Text>
            </View>
          </View>

          <View style={ticketsStyles.dashedDivider} />

          {/* Total Fare Row */}
          <View style={ticketsStyles.detailTotalRow}>
            <Text style={isDark ? ticketsStyles.detailTotalLabelDark : ticketsStyles.detailTotalLabelLight}>
              Total Fare:
            </Text>
            <Text style={ticketsStyles.detailTotalValue}>{totalPrice}</Text>
          </View>

          {/* Large QR Code Container */}
          <View style={ticketsStyles.largeQrContainer}>
            <QrCode size={180} color="#0B1F4D" />
          </View>

          <Text style={ticketsStyles.qrNoteText}>
            <Text style={ticketsStyles.qrNoteHighlight}>Note:</Text> Present this QR code to the bus conductor at the boarding gate.
          </Text>
        </View>

        {/* Bottom Action Buttons: Cancel & Download */}
        <View
          style={{
            flexDirection: 'row',
            gap: 14,
            paddingHorizontal: 16,
            marginTop: 10,
          }}
        >
          <TouchableOpacity
            onPress={() => router.back()}
            style={{
              flex: 1,
              height: 48,
              borderRadius: 14,
              borderWidth: 1.5,
              borderColor: isDark ? '#334155' : '#CBD5E1',
              justifyContent: 'center',
              alignItems: 'center',
              backgroundColor: isDark ? '#1E293B' : '#FFFFFF',
            }}
          >
            <Text style={{ fontSize: 15, fontWeight: '900', color: isDark ? '#F8FAFC' : '#0B1F4D' }}>
              Cancel
            </Text>
          </TouchableOpacity>

          <TouchableOpacity
            onPress={() => router.back()}
            style={{
              flex: 1,
              height: 48,
              borderRadius: 14,
              backgroundColor: '#0B1F4D',
              justifyContent: 'center',
              alignItems: 'center',
            }}
          >
            <Text style={{ fontSize: 15, fontWeight: '900', color: '#FFFFFF' }}>
              Download
            </Text>
          </TouchableOpacity>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
}
