import { describe, expect, it } from 'vitest';
import { MODULES } from '@/shared/constants/appData';
import {
  canUserRoleAccessModuleRoute,
  DEFAULT_MODULE_ID,
  DEFAULT_MODULE_ROUTE_PATH,
  getDefaultRoutePathForRole,
  getModuleByRoutePath,
  getModuleIdByRoutePath,
  getModuleRoutePath,
} from '@/shared/constants/moduleRoutes';

describe('module route metadata', () => {
  it('maps every module id to a unique route path', () => {
    const paths = MODULES.map((module) => module.routePath);

    expect(new Set(paths).size).toBe(MODULES.length);
    expect(MODULES.every((module) => module.routePath.startsWith('/'))).toBe(true);
  });

  it('resolves route paths back to module ids', () => {
    for (const module of MODULES) {
      expect(getModuleRoutePath(module.id)).toBe(module.routePath);
      expect(getModuleByRoutePath(module.routePath)?.id).toBe(module.id);
      expect(getModuleIdByRoutePath(module.routePath)).toBe(module.id);
    }
  });

  it('uses dashboard as the deterministic default route', () => {
    expect(DEFAULT_MODULE_ID).toBe('dashboard');
    expect(DEFAULT_MODULE_ROUTE_PATH).toBe('/dashboard');
    expect(getModuleIdByRoutePath('/')).toBe('dashboard');
    expect(getModuleIdByRoutePath('/unknown')).toBe('dashboard');
  });

  it('checks route access through module role metadata', () => {
    expect(canUserRoleAccessModuleRoute('/director-dashboard', 'teacher')).toBe(false);
    expect(canUserRoleAccessModuleRoute('/director-dashboard', 'director')).toBe(true);
    expect(canUserRoleAccessModuleRoute('/classroom-timer', 'teacher')).toBe(true);
    // office_manager keeps the original distinction (no instructional tools).
    expect(canUserRoleAccessModuleRoute('/classroom-timer', 'office_manager')).toBe(false);
    // external roles only reach the external pages.
    expect(canUserRoleAccessModuleRoute('/community-partnerships', 'student')).toBe(true);
    expect(canUserRoleAccessModuleRoute('/dashboard', 'student')).toBe(false);
  });

  it('lands each role on its first accessible module', () => {
    const community = MODULES.find((m) => m.id === 'community')?.routePath;
    expect(getDefaultRoutePathForRole('teacher')).toBe(DEFAULT_MODULE_ROUTE_PATH);
    expect(getDefaultRoutePathForRole('director')).toBe(DEFAULT_MODULE_ROUTE_PATH);
    // student/guardian cannot see the dashboard, so they land on the first
    // external page instead.
    expect(getDefaultRoutePathForRole('student')).toBe(community);
    expect(getDefaultRoutePathForRole('guardian')).toBe(community);
  });
});
