/**
 * Divisi Metrics — Sidebar navigation items
 *
 * Each item declares the roles that may see it.  The Sidebar component filters
 * this list against the current user role at render time.
 */

export type UserRole = 'Super_Admin' | 'Publisher_Admin' | 'Viewer' | 'Client_Portal';

/** Icon name identifier — mapped to actual SVG components in the Sidebar. */
export type IconName =
  | 'overview'
  | 'works'
  | 'composers'
  | 'payees'
  | 'contracts'
  | 'income'
  | 'transactions'
  | 'costs'
  | 'statements'
  | 'suspense'
  | 'audit'
  | 'settings'
  | 'home'
  | 'analytics'
  | 'songs'
  | 'documents'
  | 'chart-overview'
  | 'top-songs'
  | 'income-groups'
  | 'territories'
  | 'sources';

export type NavSection = 'favorites' | 'dashboards' | 'pages' | 'analytics';

export interface NavItem {
  label: string;
  href: string;
  icon: IconName;
  roles: readonly UserRole[];
  section?: NavSection;
}

/** All roles shorthand */
const ALL_ROLES: readonly UserRole[] = ['Super_Admin', 'Publisher_Admin', 'Viewer'] as const;

/** Publisher_Admin + Super_Admin (everything except Viewer-only) */
const ADMIN_ROLES: readonly UserRole[] = ['Super_Admin', 'Publisher_Admin'] as const;

/** Super_Admin only */
const SUPER_ONLY: readonly UserRole[] = ['Super_Admin'] as const;

/** Client Portal only */
const CLIENT_PORTAL_ONLY: readonly UserRole[] = ['Client_Portal'] as const;

/**
 * Complete sidebar navigation list.
 *
 * Role visibility rules (from Requirement 3.7):
 *  - Viewer      → Overview, Income, Statements, Costs
 *  - Publisher_Admin → all except Audit
 *  - Super_Admin → all
 */
export const navItems: readonly NavItem[] = [
  { label: 'Overview',     href: '/overview',     icon: 'overview',     roles: ALL_ROLES,    section: 'favorites' },
  { label: 'Works',        href: '/works',        icon: 'works',        roles: ADMIN_ROLES,  section: 'pages' },
  { label: 'Composers',    href: '/composers',    icon: 'composers',    roles: ADMIN_ROLES,  section: 'pages' },
  { label: 'Payees',       href: '/payees',       icon: 'payees',       roles: ADMIN_ROLES,  section: 'pages' },
  { label: 'Contracts',    href: '/contracts',    icon: 'contracts',    roles: ADMIN_ROLES,  section: 'pages' },
  { label: 'Income',       href: '/income',       icon: 'income',       roles: ALL_ROLES,    section: 'dashboards' },
  { label: 'Transactions', href: '/transactions', icon: 'transactions', roles: ADMIN_ROLES,  section: 'pages' },
  { label: 'Costs',        href: '/costs',        icon: 'costs',        roles: ALL_ROLES,    section: 'dashboards' },
  { label: 'Statements',   href: '/statements',   icon: 'statements',   roles: ALL_ROLES,    section: 'dashboards' },
  { label: 'Suspense',     href: '/suspense',     icon: 'suspense',     roles: ADMIN_ROLES,  section: 'dashboards' },
  { label: 'Audit',        href: '/audit',        icon: 'audit',        roles: SUPER_ONLY,   section: 'pages' },
  // Client Portal items
  { label: 'Inicio',               href: '/portal',                        icon: 'home',           roles: CLIENT_PORTAL_ONLY, section: 'favorites' },
  { label: 'Overview',             href: '/portal/analytics',              icon: 'chart-overview', roles: CLIENT_PORTAL_ONLY, section: 'analytics' },
  { label: 'Top Songs',            href: '/portal/analytics/top-songs',    icon: 'top-songs',      roles: CLIENT_PORTAL_ONLY, section: 'analytics' },
  { label: 'Income Groups',        href: '/portal/analytics/income-groups', icon: 'income-groups', roles: CLIENT_PORTAL_ONLY, section: 'analytics' },
  { label: 'Territories',          href: '/portal/analytics/territories',  icon: 'territories',   roles: CLIENT_PORTAL_ONLY, section: 'analytics' },
  { label: 'Exploitation Sources', href: '/portal/analytics/sources',      icon: 'sources',       roles: CLIENT_PORTAL_ONLY, section: 'analytics' },
  { label: 'Canciones',            href: '/portal/songs',                  icon: 'songs',         roles: CLIENT_PORTAL_ONLY, section: 'pages' },
  { label: 'Documentos',           href: '/portal/documents',              icon: 'documents',     roles: CLIENT_PORTAL_ONLY, section: 'pages' },
] as const;

/** Bottom-pinned sidebar items (separated visually from the main nav). */
export const bottomNavItems: readonly NavItem[] = [
  { label: 'Settings',    href: '/settings',         icon: 'settings',    roles: ['Super_Admin', 'Publisher_Admin', 'Viewer'] as const },
  { label: 'Settings',    href: '/portal/settings',  icon: 'settings',    roles: ['Client_Portal'] as const },
] as const;

/**
 * Return the main navigation items visible for a given role.
 */
export function getNavItemsForRole(role: UserRole): NavItem[] {
  return navItems.filter((item) => item.roles.includes(role));
}

/**
 * Return the bottom navigation items visible for a given role.
 */
export function getBottomNavItemsForRole(role: UserRole): NavItem[] {
  return bottomNavItems.filter((item) => item.roles.includes(role));
}

/**
 * Group navigation items by their section assignment.
 * Items without a section default to 'pages'.
 */
export function groupNavItemsBySection(items: NavItem[]): Record<NavSection, NavItem[]> {
  const grouped: Record<NavSection, NavItem[]> = {
    favorites: [],
    dashboards: [],
    pages: [],
    analytics: [],
  };

  for (const item of items) {
    const section = item.section ?? 'pages';
    grouped[section].push(item);
  }

  return grouped;
}
