import fs from 'node:fs';
import path from 'node:path';

import { primaryNavigation, supportNavigation } from '@/lib/portal-navigation';

const appRoot = '/home/intlc/projects/Sankofa/portal/src/app';

function hasBackingRoute(href: string): boolean {
  const direct =
    href === '/'
      ? path.join(appRoot, 'page.tsx')
      : path.join(appRoot, href.replace(/^\//, ''), 'page.tsx');
  if (fs.existsSync(direct)) return true;

  if (href.startsWith('/admin/')) {
    return fs.existsSync(path.join(appRoot, 'admin/[section]/page.tsx'));
  }

  if (href.startsWith('/partner/')) {
    return fs.existsSync(path.join(appRoot, 'partner/[section]/page.tsx'));
  }

  if (href.startsWith('/help/')) {
    return fs.existsSync(path.join(appRoot, 'help/[section]/page.tsx'));
  }

  return false;
}

describe('portal navigation integrity', () => {
  it('backs every primary navigation entry with a route', () => {
    for (const item of primaryNavigation) {
      expect(hasBackingRoute(item.href)).toBe(true);
    }
  });

  it('backs every support navigation entry with a route', () => {
    for (const item of supportNavigation) {
      expect(hasBackingRoute(item.href)).toBe(true);
    }
  });
});
