import React, { useState } from 'react';
import { View, Text, ScrollView, Alert } from 'react-native';
import { useAppTheme } from '@theme';
import { ScreenHeader, PrimaryButton } from '@components';
import { useAppDispatch, useAppSelector } from '@store';
import { completeJob } from '@store/commonReducers/job';
import { useNavigation } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { AppStackParamList } from '@navigation/navigationTypes';
import { 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 activeJob = useAppSelector((state) => state.job.activeJob);
  const [isLoading, setIsLoading] = useState(false);

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

  const handleCompleteDelivery = () => {
    setIsLoading(true);
    setTimeout(() => {
      setIsLoading(false);
      dispatch(completeJob());
      navigation.navigate(RouteNames.DeliveryCompleted);
    }, 1200);
  };

  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.dropName}</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>
          {items.map((item) => (
            <View key={item.id} style={styles.itemRow}>
              <Text style={styles.itemName}>{item.name}</Text>
              <Text style={styles.itemQty}>x{item.qty}</Text>
            </View>
          ))}
        </View>

        {/* Payment Collect Details */}
        <View style={styles.paymentCard}>
          <Text style={styles.paymentLabel}>Cash to Collect</Text>
          <Text style={styles.paymentVal}>₹0.00 (Online Paid)</Text>
        </View>

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

export default DeliverOrderScreen;
