import React from 'react';
import { Tabs } from 'expo-router';
import { useTheme } from '@/constants/ThemeContext';
import { useLanguage } from '@/i18n/LanguageContext';
import { View, Text } from 'react-native';
import { Home, Building2, Ticket, User } from 'lucide-react-native';
import { ThemeColors } from '@/constants/colors';

interface TabIconProps {
  focused: boolean;
  icon: React.ComponentType<{ size: number; color: string; strokeWidth?: number }>;
  title: string;
  colors: ThemeColors;
}

const TabIcon = ({ focused, icon: IconComponent, title, colors }: TabIconProps) => {
  const activeColor = colors.tabBarActive;
  const inactiveColor = colors.tabBarInactive;

  return (
    <View style={{ alignItems: 'center', justifyContent: 'center', minWidth: 60, paddingVertical: 2 }}>
      <View
        style={{
          alignItems: 'center',
          justifyContent: 'center',
          paddingVertical: 3,
          paddingHorizontal: 12,
          borderRadius: 14,
          backgroundColor: focused ? colors.tabBarHighlight : 'transparent',
          marginBottom: 3,
        }}
      >
        <IconComponent
          size={20}
          color={focused ? activeColor : inactiveColor}
          strokeWidth={focused ? 2.4 : 1.8}
        />
      </View>
      <Text
        numberOfLines={1}
        style={{
          color: focused ? activeColor : inactiveColor,
          fontSize: 10.5,
          fontWeight: focused ? '700' : '500',
          letterSpacing: -0.2,
        }}
      >
        {title}
      </Text>
    </View>
  );
};

export default function TabsLayout() {
  const { colors, isDark } = useTheme();
  const { t } = useLanguage();

  return (
    <Tabs
      screenOptions={{
        headerShown: false,
        tabBarShowLabel: false,
        tabBarItemStyle: {
          width: '100%',
          height: '100%',
          justifyContent: 'center',
          alignItems: 'center',
          marginTop: 10,
        },
        tabBarStyle: {
          backgroundColor: colors.tabBar,
          borderColor: colors.tabBarBorder,
          borderWidth: 1.5,
          borderRadius: 36,
          marginHorizontal: 16,
          marginBottom: 16,
          height: 64,
          position: 'absolute',
          overflow: 'hidden',
          shadowColor: colors.navy,
          shadowOffset: { width: 0, height: 4 },
          shadowOpacity: isDark ? 0.3 : 0.08,
          shadowRadius: 12,
          elevation: 6,
        },
      }}
    >
      {/* 1. Home Tab */}
      <Tabs.Screen
        name="index"
        options={{
          title: t('tabHome') || 'Home',
          tabBarIcon: ({ focused }) => (
            <TabIcon focused={focused} icon={Home} title={t('tabHome') || 'Home'} colors={colors} />
          ),
        }}
      />

      {/* 2. Agencies Tab */}
      <Tabs.Screen
        name="agencies"
        options={{
          title: t('tabAgencies') || 'Agencies',
          tabBarIcon: ({ focused }) => (
            <TabIcon focused={focused} icon={Building2} title={t('tabAgencies') || 'Agencies'} colors={colors} />
          ),
        }}
      />

      {/* 3. Tickets Tab */}
      <Tabs.Screen
        name="tickets"
        options={{
          title: t('tabTickets') || 'Tickets',
          tabBarIcon: ({ focused }) => (
            <TabIcon focused={focused} icon={Ticket} title={t('tabTickets') || 'Tickets'} colors={colors} />
          ),
        }}
      />

      {/* 4. Profile Tab */}
      <Tabs.Screen
        name="profile"
        options={{
          title: t('tabProfile') || 'Profile',
          tabBarIcon: ({ focused }) => (
            <TabIcon focused={focused} icon={User} title={t('tabProfile') || 'Profile'} colors={colors} />
          ),
        }}
      />

      {/* Hidden from bottom bar: Bookings & Notifications (accessed via header bell) */}
      <Tabs.Screen
        name="bookings"
        options={{
          href: null,
        }}
      />
      <Tabs.Screen
        name="notifications"
        options={{
          href: null,
        }}
      />
    </Tabs>
  );
}
