import { Platform } from "react-native";
import { NativeTabs } from "expo-router/unstable-native-tabs";

import { AppLockOverlay } from "@/components/AppLockOverlay";
import { InvoiceReminderSync } from "@/components/InvoiceReminderSync";
import { OnboardingGate } from "@/components/OnboardingGate";
import { ShortcutHandler } from "@/components/ShortcutHandler";
import { TimeClockLiveActivitySync } from "@/components/time-clock/TimeClockLiveActivitySync";
import { useAppTheme } from "@/contexts/ThemeContext";
import { AppLockProvider } from "@/contexts/AppLockContext";

export default function AppLayout() {
  const { colors, isDark } = useAppTheme();

  const tintColor = colors.primary;
  const labelColor = colors.mutedForeground;
  const tabContentStyle = { backgroundColor: colors.background };
  const tabBarBlur =
    Platform.OS === "ios"
      ? isDark
        ? "systemChromeMaterialDark"
        : "systemChromeMaterialLight"
      : undefined;

  return (
    <AppLockProvider>
      <NativeTabs
        tintColor={tintColor}
        iconColor={{
          default: labelColor,
          selected: tintColor,
        }}
        labelStyle={{ color: labelColor }}
        blurEffect={tabBarBlur}
        disableTransparentOnScrollEdge
        backgroundColor={Platform.OS === "android" ? colors.background : undefined}
      >
        <NativeTabs.Trigger name="index" contentStyle={tabContentStyle} disableAutomaticContentInsets>
          <NativeTabs.Trigger.Icon
            sf={{ default: "house", selected: "house.fill" }}
            md="home"
          />
          <NativeTabs.Trigger.Label>Home</NativeTabs.Trigger.Label>
        </NativeTabs.Trigger>

        <NativeTabs.Trigger name="timer" contentStyle={tabContentStyle} disableAutomaticContentInsets>
          <NativeTabs.Trigger.Icon
            sf={{ default: "timer", selected: "timer" }}
            md="timer"
          />
          <NativeTabs.Trigger.Label>Timer</NativeTabs.Trigger.Label>
        </NativeTabs.Trigger>

        <NativeTabs.Trigger name="entities" contentStyle={tabContentStyle} disableAutomaticContentInsets>
          <NativeTabs.Trigger.Icon
            sf={{ default: "square.stack.3d.up", selected: "square.stack.3d.up.fill" }}
            md="corporate_fare"
          />
          <NativeTabs.Trigger.Label>Entities</NativeTabs.Trigger.Label>
        </NativeTabs.Trigger>

        <NativeTabs.Trigger name="invoices" contentStyle={tabContentStyle} disableAutomaticContentInsets>
          <NativeTabs.Trigger.Icon
            sf={{ default: "doc.text", selected: "doc.text.fill" }}
            md="description"
          />
          <NativeTabs.Trigger.Label>Invoices</NativeTabs.Trigger.Label>
        </NativeTabs.Trigger>

        <NativeTabs.Trigger name="more" role="more" contentStyle={tabContentStyle} disableAutomaticContentInsets>
          <NativeTabs.Trigger.Icon
            sf={{ default: "ellipsis.circle", selected: "ellipsis.circle.fill" }}
            md="more_horiz"
          />
          <NativeTabs.Trigger.Label>More</NativeTabs.Trigger.Label>
        </NativeTabs.Trigger>
      </NativeTabs>
      <OnboardingGate />
      <InvoiceReminderSync />
      <TimeClockLiveActivitySync />
      <ShortcutHandler />
      <AppLockOverlay />
    </AppLockProvider>
  );
}
