import { describe, expect, it } from 'vitest';
import {
  canAccessModule,
  getAccessibleModuleId,
  getAccessibleModules,
  getSidebarCampusInitial,
  getSidebarRoleLabel,
  shouldShowMobileSidebarOverlay,
} from '@/business/app-shell/selectors';
import type { Module } from '@/shared/types/app';

const modules: readonly Module[] = [
  {
    id: 'dashboard',
    name: 'Dashboard',
    icon: 'home',
    roles: ['teacher', 'support_staff', 'office_manager', 'director', 'superintendent'],
    color: 'text-blue-400',
    routePath: '/dashboard',
  },
  {
    id: 'director',
    name: 'Director',
    icon: 'users',
    roles: ['director', 'superintendent'],
    color: 'text-emerald-400',
    routePath: '/director-dashboard',
  },
];

describe('app-shell selectors', () => {
  it('checks module access from module role metadata', () => {
    expect(canAccessModule(modules, 'dashboard', 'teacher')).toBe(true);
    expect(canAccessModule(modules, 'director', 'teacher')).toBe(false);
    expect(canAccessModule(modules, 'director', 'superintendent')).toBe(true);
  });

  it('falls back to dashboard when the requested module is unavailable', () => {
    expect(getAccessibleModuleId(modules, 'director', 'teacher')).toBe('dashboard');
    expect(getAccessibleModuleId(modules, 'director', 'director')).toBe('director');
  });

  it('returns modules available to the selected role', () => {
    expect(getAccessibleModules(modules, 'teacher').map((module) => module.id)).toEqual(['dashboard']);
    expect(getAccessibleModules(modules, 'director').map((module) => module.id)).toEqual(['dashboard', 'director']);
  });

  it('formats sidebar role labels through shared auth role labels', () => {
    expect(getSidebarRoleLabel('support_staff')).toBe('Support Staff');
    expect(getSidebarRoleLabel('office_manager')).toBe('Office Manager');
  });

  it('returns a campus initial when campus branding is available', () => {
    expect(getSidebarCampusInitial({
      id: 'campus-1',
      mascot: 'Tigers',
      fullName: 'Tigers Campus',
      color: 'bg-orange-500',
      bgGradient: 'from-orange-500 to-amber-500',
      borderColor: 'border-orange-500/30',
      textColor: 'text-orange-400',
      bgLight: 'bg-orange-500/10',
      description: 'Campus branding',
    })).toBe('T');
    expect(getSidebarCampusInitial()).toBeNull();
  });

  it('shows the mobile sidebar overlay only when mobile sidebar is open on mobile', () => {
    expect(shouldShowMobileSidebarOverlay(true, true)).toBe(true);
    expect(shouldShowMobileSidebarOverlay(true, false)).toBe(false);
    expect(shouldShowMobileSidebarOverlay(false, true)).toBe(false);
  });
});
