import React from 'react';
import { Link, useLocation } from 'react-router-dom';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Button } from '@/components/ui/button';
import { RefreshCw } from 'lucide-react';
import { useDashboardPeriod } from '@/hooks/useDashboardPeriod';
import { DummyDataToggle } from '../DummyDataToggle';
import { __ } from '@/lib/i18n';
import { useQueryClient } from '@tanstack/react-query';
import type { SubItem } from '@/nav/tree';

type Props = { items?: SubItem[]; fullscreen?: boolean; headerVisible?: boolean };

export default function DashboardSubmenuBar({ items = [], fullscreen = false, headerVisible = true }: Props) {
  const { period, setPeriod, useDummy } = useDashboardPeriod();
  const { pathname } = useLocation();
  const queryClient = useQueryClient();
  
  const handleRefresh = () => {
    queryClient.invalidateQueries({ queryKey: ['analytics'] });
  };

  if (items.length === 0) return null;

  // Calculate top position based on fullscreen state
  // Fullscreen: top-0 (no contextual headers, submenu is first element)
  // Normal: top-[calc(7rem+32px)] (below WP admin bar + menu bar)
  const topClass = fullscreen ? 'top-0' : 'top-[calc(7rem+32px)]';

  return (
    <div data-submenubar className={`border-b border-border bg-background md:bg-background/95 md:backdrop-blur md:supports-[backdrop-filter]:bg-background/60 sticky ${topClass} z-20`}>
      <div className="px-4 py-2">
        <div className="flex flex-col xl:flex-row items-center justify-between gap-4">
          {/* Submenu Links */}
          <div className="flex gap-2 overflow-x-auto no-scrollbar w-full flex-shrink">
            {items.map((it) => {
              const key = `${it.label}-${it.path || it.href}`;
              // Fix: Always use exact match to prevent first submenu from being always active
              const isActive = !!it.path && pathname === it.path;
              const cls = [
                'ui-ctrl inline-flex items-center gap-2 rounded-md px-2.5 py-1.5 border text-sm whitespace-nowrap',
                'focus:outline-none focus:ring-0 focus:shadow-none',
                isActive ? 'bg-accent text-accent-foreground' : 'hover:bg-accent hover:text-accent-foreground',
              ].join(' ');

              if (it.mode === 'spa' && it.path) {
                return (
                  <Link key={key} to={it.path} className={cls} data-discover>
                    {it.label}
                  </Link>
                );
              }

              if (it.mode === 'bridge' && it.href) {
                return (
                  <a key={key} href={it.href} className={cls} data-discover>
                    {it.label}
                  </a>
                );
              }

              return null;
            })}
          </div>

          {/* Period Selector, Refresh & Dummy Toggle */}
          <div className="flex justify-end xl:items-center gap-2 flex-shrink-0 w-full xl:w-auto flex-shrink">
            <Select value={period} onValueChange={setPeriod}>
              <SelectTrigger className="w-full xl:w-[140px] h-8">
                <SelectValue />
              </SelectTrigger>
              <SelectContent>
                <SelectItem value="7">{__('Last 7 days')}</SelectItem>
                <SelectItem value="14">{__('Last 14 days')}</SelectItem>
                <SelectItem value="30">{__('Last 30 days')}</SelectItem>
                <SelectItem value="all">{__('All Time')}</SelectItem>
              </SelectContent>
            </Select>
            {!useDummy && (
              <Button
                variant="outline"
                size="sm"
                onClick={handleRefresh}
                className="h-8"
                title={__('Refresh data (cached for 5 minutes)')}
              >
                <RefreshCw className="w-4 h-4 mr-1" />
                {__('Refresh')}
              </Button>
            )}
            <DummyDataToggle />
          </div>
        </div>
      </div>
    </div>
  );
}
