import React from 'react';
import { View, Text, Alert } from 'react-native';
import { useAppTheme } from '@theme';
import { PrimaryButton } from '@components';
import { useAppDispatch, useAppSelector } from '@store';
import { advanceJobStatus } from '@store/commonReducers/job';
import { useNavigation } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { AppStackParamList } from '@navigation/navigationTypes';
import { RouteNames, JobStatus } from '@utils/constants';
import { getStyles } from './liveTrackingScreen.styles';

export const LiveTrackingScreen: 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 handleArrived = () => {
    dispatch(advanceJobStatus(JobStatus.ArrivedAtCustomer));
    navigation.navigate(RouteNames.ArrivedAtCustomer);
  };

  const handleContact = () => {
    Alert.alert('Contact Customer', `Calling ${activeJob?.dropName || 'Customer'} (+91 99887 76655)...`);
  };

  if (!activeJob) return null;

  return (
    <View style={styles.container}>
      {/* Top Floating ETA Card */}
      <View style={styles.etaCard}>
        <Text style={styles.etaTitle}>Arriving in 8 mins</Text>
        <Text style={styles.etaSub}>Distance: {activeJob.distance} km • Speed: 24 km/h</Text>
      </View>

      {/* Map Background Placeholder */}
      <View style={styles.mapPlaceholder}>
        <Text style={styles.placeholderText}>[ Map Placeholder ]</Text>
      </View>

      {/* Customer Location Bottom Card */}
      <View style={styles.bottomCard}>
        <Text style={styles.cardTitle}>Deliver to</Text>
        <Text style={styles.customerName}>{activeJob.dropName}</Text>
        <Text style={styles.customerAddress}>{activeJob.dropAddress}</Text>

        <View style={styles.btnRow}>
          <PrimaryButton
            title="Call"
            onPress={handleContact}
            style={styles.contactBtn}
            textStyle={{ color: colors.text }}
          />
          <PrimaryButton
            title="Arrived at Location"
            onPress={handleArrived}
            style={styles.arrivedBtn}
          />
        </View>
      </View>
    </View>
  );
};

export default LiveTrackingScreen;
