import { Link, NavLink, Outlet, useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import { useEffect, useState, useCallback, useRef } from 'react';
import {
LayoutDashboard, Users, Sparkles, MailX, Search, RefreshCw, LogOut,
ChevronLeft, ChevronRight, ChevronDown, X, Plus, Wand2, ShieldCheck,
ScrollText, ListChecks,
} from 'lucide-react';
import { AuthApi, SyncApi, AnalyticsApi } from '../api/client.js';
import Logo from './Logo.jsx';
import DevBanner from './DevBanner.jsx';
import SyncStatus from './SyncStatus.jsx';
import DigestToggle from './DigestToggle.jsx';
import useSavedSearches from '../hooks/useSavedSearches.js';
import { cn } from '../lib/utils.js';
import {
Button, Input, Separator, ThemeToggle, Tooltip, TooltipContent, TooltipTrigger,
DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem,
DropdownMenuLabel, DropdownMenuSeparator,
} from './ui';
// ── Folder definitions ────────────────────────────────────────────────────────
const MAILBOX = [
{ slug: 'inbox', icon: '📥', label: 'Inbox', countKey: 'inbox' },
{ slug: 'allmail', icon: '📬', label: 'All Mail', countKey: 'allMail' },
{ slug: 'starred', icon: '⭐', label: 'Starred', countKey: 'starred' },
{ slug: 'sent', icon: '📤', label: 'Sent', countKey: 'sent' },
{ slug: 'drafts', icon: '✏️', label: 'Drafts', countKey: 'drafts' },
{ slug: 'archive', icon: '📦', label: 'Archive', countKey: null },
{ slug: 'spam', icon: '🚫', label: 'Spam', countKey: 'spam' },
{ slug: 'trash', icon: '🗑️', label: 'Trash', countKey: 'trash' },
{ slug: 'unlabeled', icon: '🏷️', label: 'Unlabeled', countKey: null },
{ slug: 'pinned', icon: '📌', label: 'Pinned', countKey: null },
{ slug: 'readlater', icon: '🔖', label: 'Read Later', countKey: null },
];
const FAV_SPECIALS = {
inbox: { slug: 'inbox', icon: '📥', label: 'Inbox', countKey: 'inbox', type: 'mailbox' },
unread: { slug: 'unread', icon: '🔵', label: 'Unread Mail', countKey: 'unread', type: 'filter' },
large: { slug: 'large', icon: '📎', label: 'Large Mail', countKey: 'large', type: 'filter' },
old: { slug: 'old', icon: '🕰️', label: 'Old Mail', countKey: 'old', type: 'filter' },
readlater: { slug: 'readlater', icon: '🔖', label: 'Read Later', countKey: null, type: 'filter' },
};
const DEFAULT_FAV_SLUGS = ['inbox', 'unread', 'large', 'old', 'readlater'];
const SMART_FOLDERS = [
{ slug: 'automated', icon: '🤖', label: 'Automated', countKey: 'automated' },
{ slug: 'noreply', icon: '🔇', label: 'No-Reply', countKey: 'noreply' },
{ slug: 'shopping', icon: '🛍️', label: 'Online Shopping', countKey: 'shopping' },
{ slug: 'gaming', icon: '🎮', label: 'Gaming', countKey: 'gaming' },
{ slug: 'finance', icon: '💳', label: 'Finance & Insurance', countKey: 'finance' },
{ slug: 'sales', icon: '🏷️', label: 'Seasonal Sales', countKey: 'sales' },
{ slug: 'ridesharing', icon: '🚗', label: 'Ride Sharing', countKey: 'ridesharing' },
{ slug: 'food', icon: '🍕', label: 'Food Delivery', countKey: 'food' },
{ slug: 'social', icon: '📱', label: 'Social Notifications', countKey: 'social' },
{ slug: 'wellness', icon: '🏃', label: 'Wellness & Sport', countKey: 'wellness' },
{ slug: 'travel', icon: '✈️', label: 'Travel', countKey: 'travel' },
{ slug: 'subscriptions',icon: '🔄', label: 'Subscriptions & SaaS', countKey: 'subscriptions' },
{ slug: 'parcels', icon: '📦', label: 'Parcels & Delivery', countKey: 'parcels' },
{ slug: 'recruitment', icon: '💼', label: 'Recruitment & Jobs', countKey: 'recruitment' },
{ slug: 'events', icon: '🎟️', label: 'Events & Tickets', countKey: 'events' },
{ slug: 'security', icon: '🔐', label: 'Security & Alerts', countKey: 'security' },
{ slug: 'healthcare', icon: '🏥', label: 'Healthcare', countKey: 'healthcare' },
{ slug: 'education', icon: '🎓', label: 'Education', countKey: 'education' },
{ slug: 'news', icon: '📰', label: 'News & Media', countKey: 'news' },
{ slug: 'property', icon: '🏠', label: 'Property & Utilities', countKey: 'property' },
{ slug: 'charity', icon: '❤️', label: 'Charities', countKey: 'charity' },
{ slug: 'government', icon: '🏛️', label: 'Government', countKey: 'government' },
{ slug: 'crypto', icon: '📈', label: 'Crypto & Investing', countKey: 'crypto' },
{ slug: 'family', icon: '👨👩👧', label: 'Family & School', countKey: 'family' },
];
// Primary app navigation (top of the sidebar). Automation pages are placeholders
// until their feature work lands (see docs/specs); links are kept here so the IA
// is visible, and they no-op gracefully to routes added later.
const PRIMARY_NAV = [
{ to: '/app', label: 'Dashboard', icon: LayoutDashboard, end: true },
{ to: '/app/senders', label: 'Senders', icon: Users },
{ to: '/app/cleanup', label: 'Cleanup', icon: Sparkles },
{ to: '/app/unsubscribe', label: 'Unsubscribe', icon: MailX },
];
// ── Helpers ───────────────────────────────────────────────────────────────────
const fmtCount = (n) => {
if (!n) return null;
return n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n);
};
const LS_FAV = 'ii:fav-slugs';
const LS_OPEN = 'ii:sidebar-open';
const LS_SECTS = 'ii:sidebar-sections';
const loadFavs = () => {
try { return JSON.parse(localStorage.getItem(LS_FAV)) ?? DEFAULT_FAV_SLUGS; }
catch { return DEFAULT_FAV_SLUGS; }
};
const saveFavs = (v) => localStorage.setItem(LS_FAV, JSON.stringify(v));
const loadSections = () => {
try { return JSON.parse(localStorage.getItem(LS_SECTS)) ?? { fav: true, mailbox: true, smart: true, saved: true }; }
catch { return { fav: true, mailbox: true, smart: true, saved: true }; }
};
const saveSections = (v) => localStorage.setItem(LS_SECTS, JSON.stringify(v));
// ── Sidebar sub-components ──────────────────────────────────────────────────────
function SectionHead({ label, open, onToggle, collapsed }) {
if (collapsed) return