import React, { useState } from 'react';
import { View, Text, ScrollView, Alert } from 'react-native';
import { useAppTheme } from '@theme';
import { ScreenHeader, PrimaryButton } from '@components';
import { changeOrderStatusThunk, useAppDispatch, useAppSelector } from '@store';
import { completeJob } from '@store/commonReducers/job';
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { AppStackParamList } from '@navigation/navigationTypes';
import { JobStatus, RouteNames } from '@utils/constants';
import { getStyles } from './deliverOrderScreen.styles';

export const DeliverOrderScreen: React.FC = () => {
  const { colors } = useAppTheme();
  const styles = getStyles(colors);
  const dispatch = useAppDispatch();
  const navigation =
    useNavigation<NativeStackNavigationProp<AppStackParamList>>();
  const route =
    useRoute<RouteProp<AppStackParamList, RouteNames.OrderPickedUp>>();
  const { jobId } = route.params;

  const { activeDeliveries } = useAppSelector(state => state.deliveries);
  const activeJob = activeDeliveries?.delivery;

  const [isLoading, setIsLoading] = useState(false);

  // const items = [
  //   { id: 0, name: 'Cappuccino (Medium)', qty: 1 },
  //   { id: 1, name: 'Chocolate Croissant', qty: 1 },
  // ];

  const handleCompleteDelivery = () => {
    dispatch(
      changeOrderStatusThunk({
        deliveryId: jobId,
        action: JobStatus.Delivered,
      }),
    )
      .unwrap()
      .then(() => {
        // dispatch(completeJob());
        navigation.navigate(RouteNames.DeliveryCompleted);
      });
  };

  if (!activeJob) return null;

  return (
    <View style={styles.container}>
      <ScreenHeader
        title="Deliver Order"
        showBack={true}
        onBack={() => navigation.goBack()}
      />

      <ScrollView
        contentContainerStyle={styles.scrollContainer}
        showsVerticalScrollIndicator={false}
      >
        {/* Customer Detail Card */}
        <View style={styles.card}>
          <View style={styles.row}>
            <Text style={styles.label}>Customer Name</Text>
            <Text style={styles.value}>
              {activeJob?.order?.customer?.user?.name}
            </Text>
          </View>
          <View style={[styles.row, { marginTop: 10 }]}>
            <Text style={styles.label}>Order ID</Text>
            <Text style={styles.value}>{activeJob?.orderId}</Text>
          </View>
        </View>

        {/* Items Summary list */}
        <View style={styles.section}>
          <Text style={styles.sectionTitle}>Items Details</Text>
          {activeJob?.order?.orderItems?.map(item => (
            <View key={item.id} style={styles.itemRow}>
              <Text style={styles.itemName}>{item.name}</Text>
              <Text style={styles.itemQty}>x{item.quantity}</Text>
            </View>
          ))}
        </View>

        {/* Payment Collect Details */}
        <View style={styles.paymentCard}>
          <Text style={styles.paymentLabel}>
            {activeJob?.order?.paymentMethod === 'WALLET' ||
            activeJob?.order?.paymentMethod === 'UPI'
              ? 'Already Paid'
              : 'Collect from Customer'}
          </Text>
          <Text style={styles.paymentVal}>
            ₹{activeJob?.order?.totalAmount}
          </Text>
        </View>

        <PrimaryButton
          title="Complete Delivery"
          onPress={handleCompleteDelivery}
          isLoading={isLoading}
          style={styles.completeButton}
        />
      </ScrollView>
    </View>
  );
};

export default DeliverOrderScreen;
