'use client';

import { motion, useReducedMotion } from 'framer-motion';
import { Bell, Building2, Shield, User, Users, type LucideIcon } from 'lucide-react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';

import { usePermission } from '@/hooks/use-permission';
import { cn } from '@/lib/utils';
import type { Permission } from '@/shared/permissions';

const tabs: Array<{ href: string; label: string; icon: LucideIcon; permission?: Permission }> = [
  { href: '/settings/profile', label: 'Profile', icon: User },
  { href: '/settings/security', label: 'Security', icon: Shield },
  { href: '/settings/company', label: 'Company', icon: Building2, permission: 'tenant:settings' },
  { href: '/settings/team', label: 'Team', icon: Users, permission: 'team:manage' },
  { href: '/settings/notifications', label: 'Notifications', icon: Bell, permission: 'tenant:settings' },
];

export function SettingsNav() {
  const pathname = usePathname();
  const reduce = useReducedMotion();
  const canCompany = usePermission('tenant:settings');
  const canTeam = usePermission('team:manage');
  const visible = tabs.filter((tab) => {
    if (tab.permission === 'tenant:settings') return canCompany;
    if (tab.permission === 'team:manage') return canTeam;
    return true;
  });
  return (
    <nav aria-label="Settings" className="lg:sticky lg:top-24 lg:self-start">
      {/* Mobile and tablet: horizontally scrollable pills */}
      <ul className="-mx-4 flex gap-2 overflow-x-auto px-4 pb-1 lg:hidden">
        {visible.map((tab) => {
          const active = pathname === tab.href || pathname.startsWith(`${tab.href}/`);
          const Icon = tab.icon;
          return (
            <li key={tab.href} className="shrink-0">
              <Link
                href={tab.href}
                aria-current={active ? 'page' : undefined}
                className={cn(
                  'inline-flex h-9 items-center gap-1.5 rounded-pill border px-3.5 text-small font-semibold transition-colors',
                  active ? 'border-primary/30 bg-primary-soft text-primary' : 'border-border bg-surface text-muted-foreground hover:text-foreground',
                )}
              >
                <Icon className="h-4 w-4" aria-hidden="true" />
                {tab.label}
              </Link>
            </li>
          );
        })}
      </ul>
      {/* Desktop: vertical list with a sliding indicator */}
      <ul className="hidden gap-0.5 lg:grid">
        {visible.map((tab) => {
          const active = pathname === tab.href || pathname.startsWith(`${tab.href}/`);
          const Icon = tab.icon;
          return (
            <li key={tab.href}>
              <Link
                href={tab.href}
                aria-current={active ? 'page' : undefined}
                className={cn(
                  'relative flex h-10 items-center gap-3 rounded-md px-3 text-[14px] font-medium transition-colors',
                  active ? 'bg-primary-soft text-primary' : 'text-muted-foreground hover:bg-surface-muted hover:text-foreground',
                )}
              >
                {active ? (
                  <motion.span
                    layoutId="settings-nav-indicator"
                    className="absolute left-0 top-2 h-6 w-[3px] rounded-r-full bg-primary"
                    transition={reduce ? { duration: 0 } : { type: 'spring', stiffness: 500, damping: 40 }}
                    aria-hidden="true"
                  />
                ) : null}
                <Icon className="h-[18px] w-[18px]" strokeWidth={1.75} aria-hidden="true" />
                {tab.label}
              </Link>
            </li>
          );
        })}
      </ul>
    </nav>
  );
}
