import React from 'react';
import { TouchableOpacity, StyleSheet, Platform } from 'react-native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { DrawerNavigationProp } from '@react-navigation/drawer';
import Icon from 'react-native-vector-icons/Ionicons';
import {
  LeadsScreen,
  LeadDetailsScreen,
  LeadProposalsScreen,
  LeadTasksScreen,
  LeadNotesScreen,
} from '@features';
import { route, RouteParams } from '@utils';
import { useTheme } from '@theme';
import { getStyles } from './leadsStack.styles';

export type LeadsStackParamList = Pick<
  RouteParams,
  'leadsList' | 'leadDetails' | 'leadProposals' | 'leadTasks' | 'leadNotes'
>;

const Stack = createNativeStackNavigator<LeadsStackParamList>();

export const LeadsStack = () => {
  const { theme: colors } = useTheme();
  const styles = getStyles(colors);

  return (
    <Stack.Navigator
      screenOptions={{
        headerStyle: {
          backgroundColor: colors.header,
        },
        headerTitleStyle: {
          fontSize: 17,
          fontWeight: '700',
          color: colors.text,
        },
        headerTitleAlign: 'center',
        headerTintColor: colors.text,
        headerShadowVisible: false,
      }}>
      <Stack.Screen
        name={route.leadsList}
        component={LeadsScreen}
        options={({ navigation }) => ({
          headerTitle: 'My Leads',
          headerLeft: () => (
            <TouchableOpacity
              style={styles.drawerButton}
              activeOpacity={0.7}
              onPress={() => {
                const parentNav = navigation.getParent<DrawerNavigationProp<any>>();
                if (parentNav) {
                  parentNav.openDrawer();
                } else {
                  (navigation as any).openDrawer?.();
                }
              }}>
              <Icon name="menu-outline" size={20} color={colors.text} />
            </TouchableOpacity>
          ),
        })}
      />
      <Stack.Screen
        name={route.leadDetails}
        component={LeadDetailsScreen}
        options={{ headerTitle: 'Lead Details' }}
      />
      <Stack.Screen
        name={route.leadProposals}
        component={LeadProposalsScreen}
        options={{ headerTitle: 'Proposals' }}
      />
      <Stack.Screen
        name={route.leadTasks}
        component={LeadTasksScreen}
        options={{ headerTitle: 'Tasks' }}
      />
      <Stack.Screen
        name={route.leadNotes}
        component={LeadNotesScreen}
        options={{ headerTitle: 'Notes' }}
      />
    </Stack.Navigator>
  );
};
