import type { ReactNode } from 'react';

export interface AppNavItem {
  href: string;
  label: string;
  shortTitle: string;
  description: string;
  category: 'Operations' | 'Master Data' | 'People' | 'Insights' | 'Administration';
  icon: ReactNode;
  perm?: string;
  anyPerms?: string[];
  adminOnly?: boolean;
  linkedEmployeeOnly?: boolean;
}

export const APP_NAV_ITEMS: AppNavItem[] = [
  {
    href: '/dashboard',
    label: 'Dashboard',
    shortTitle: 'Home',
    description: 'Start here and open the main work areas.',
    category: 'Operations',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6" />
      </svg>
    ),
  },
  {
    href: '/customers/jobs',
    label: 'Jobs',
    shortTitle: 'Jobs',
    description: 'Review customer-linked jobs, variations, and costing entry points.',
    category: 'Master Data',
    perm: 'job.view',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
      </svg>
    ),
  },
  {
    href: '/stock',
    label: 'Stock',
    shortTitle: 'Stock',
    description: 'Open the stock workspace for materials, receipts, dispatch, and batch visibility.',
    category: 'Operations',
    anyPerms: ['material.view', 'job.view', 'transaction.stock_in', 'transaction.stock_out', 'transaction.reconcile', 'transaction.adjust'],
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" />
      </svg>
    ),
  },
  {
    href: '/suppliers',
    label: 'Suppliers',
    shortTitle: 'Suppliers',
    description: 'Keep supplier records, contacts, and trade details updated.',
    category: 'Master Data',
    perm: 'supplier.view',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5.581a1 1 0 00-.424.106A2 2 0 005 19m2 0H3m2 0h5.581A2 2 0 0010 21m0-6h6m-6 0h6m0 0v-3m0 3v3" />
      </svg>
    ),
  },
  {
    href: '/customers',
    label: 'Customers',
    shortTitle: 'Customers',
    description: 'Manage customer records and linked project relationships.',
    category: 'Master Data',
    perm: 'customer.view',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z" />
      </svg>
    ),
  },
  {
    href: '/hr',
    label: 'HR',
    shortTitle: 'HR',
    description: 'Run employees, schedules, attendance, and HR reporting.',
    category: 'People',
    perm: 'hr.employee.view',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
      </svg>
    ),
  },
  {
    href: '/me',
    label: 'My HR',
    shortTitle: 'My HR',
    description: 'Open self-service profile and attendance pages.',
    category: 'People',
    linkedEmployeeOnly: true,
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M5.121 17.804A13.937 13.937 0 0112 16c2.5 0 4.847.655 6.879 1.804M15 10a3 3 0 11-6 0 3 3 0 016 0zm6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
    ),
  },
  {
    href: '/reports/job-profitability',
    label: 'Reports',
    shortTitle: 'Reports',
    description: 'Review consumption, operational summaries, and analytics.',
    category: 'Insights',
    perm: 'report.view',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
      </svg>
    ),
  },
  {
    href: '/settings/media',
    label: 'Media',
    shortTitle: 'Media',
    description: 'Review company uploads, usage links, and filters for the media library.',
    category: 'Administration',
    perm: 'settings.manage',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path
          strokeLinecap="round"
          strokeLinejoin="round"
          strokeWidth={1.5}
          d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
        />
      </svg>
    ),
  },
  {
    href: '/settings',
    label: 'Settings',
    shortTitle: 'Settings',
    description: 'Configure company setup, print formats, and integrations.',
    category: 'Administration',
    perm: 'settings.manage',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
      </svg>
    ),
  },
  {
    href: '/admin/companies',
    label: 'Companies',
    shortTitle: 'Companies',
    description: 'Manage company records and shared tenant setup.',
    category: 'Administration',
    adminOnly: true,
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
      </svg>
    ),
  },
  {
    href: '/admin/roles',
    label: 'Roles',
    shortTitle: 'Roles',
    description: 'Control permissions and role definitions.',
    category: 'Administration',
    perm: 'role.manage',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
      </svg>
    ),
  },
  {
    href: '/admin/users',
    label: 'Users',
    shortTitle: 'Users',
    description: 'Manage ERP users, company access, and assignments.',
    category: 'Administration',
    perm: 'user.view',
    icon: (
      <svg className="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
      </svg>
    ),
  },
];

export function filterVisibleNavItems(
  items: AppNavItem[],
  options: {
    permissions: string[];
    isSuperAdmin: boolean;
    linkedEmployeeId?: string | null;
    selfServiceOnly?: boolean;
  },
) {
  return items.filter((item) => {
    if (options.selfServiceOnly && !item.linkedEmployeeOnly) return false;
    if (!options.selfServiceOnly && item.linkedEmployeeOnly && !options.linkedEmployeeId) return false;
    if (item.adminOnly) return options.isSuperAdmin;
    if (item.linkedEmployeeOnly) return Boolean(options.linkedEmployeeId);
    if (item.anyPerms?.length) {
      return options.isSuperAdmin || item.anyPerms.some((perm) => options.permissions.includes(perm));
    }
    if (item.perm) return options.isSuperAdmin || options.permissions.includes(item.perm);
    return true;
  });
}
