import { describe, it, expect } from 'vitest';
import { routeMatchers } from '../../../apps/website/lib/routing/RouteConfig';

describe('RouteConfig - routeMatchers Invariants', () => {
  describe('isPublic()', () => {
    it('should return true for exact public matches', () => {
      expect(routeMatchers.isPublic('/')).toBe(true);
      expect(routeMatchers.isPublic('/leagues')).toBe(true);
      expect(routeMatchers.isPublic('/auth/login')).toBe(true);
    });

    it('should return true for top-level detail pages (league, race, driver, team)', () => {
      expect(routeMatchers.isPublic('/leagues/123')).toBe(true);
      expect(routeMatchers.isPublic('/races/456')).toBe(true);
      expect(routeMatchers.isPublic('/drivers/789')).toBe(true);
      expect(routeMatchers.isPublic('/teams/abc')).toBe(true);
    });

    it('should return false for "leagues/create" and "teams/create" (protected)', () => {
      expect(routeMatchers.isPublic('/leagues/create')).toBe(false);
      expect(routeMatchers.isPublic('/teams/create')).toBe(false);
    });

    it('should return false for nested protected routes', () => {
      expect(routeMatchers.isPublic('/dashboard')).toBe(false);
      expect(routeMatchers.isPublic('/profile/settings')).toBe(false);
      expect(routeMatchers.isPublic('/admin/users')).toBe(false);
      expect(routeMatchers.isPublic('/sponsor/dashboard')).toBe(false);
    });

    it('should return true for sponsor signup (public)', () => {
      expect(routeMatchers.isPublic('/sponsor/signup')).toBe(true);
    });

    it('should return false for unknown routes', () => {
      expect(routeMatchers.isPublic('/unknown-route')).toBe(false);
      expect(routeMatchers.isPublic('/api/something')).toBe(false);
    });
  });

  describe('requiresRole()', () => {
    it('should return admin roles for admin routes', () => {
      const roles = routeMatchers.requiresRole('/admin');
      expect(roles).toContain('admin');
      expect(roles).toContain('super-admin');
      
      const userRoles = routeMatchers.requiresRole('/admin/users');
      expect(userRoles).toEqual(roles);
    });

    it('should return sponsor role for sponsor routes', () => {
      expect(routeMatchers.requiresRole('/sponsor/dashboard')).toEqual(['sponsor']);
      expect(routeMatchers.requiresRole('/sponsor/billing')).toEqual(['sponsor']);
    });

    it('should return null for public routes', () => {
      expect(routeMatchers.requiresRole('/')).toBeNull();
      expect(routeMatchers.requiresRole('/leagues')).toBeNull();
    });

    it('should return null for non-role protected routes', () => {
      expect(routeMatchers.requiresRole('/dashboard')).toBeNull();
      expect(routeMatchers.requiresRole('/profile')).toBeNull();
    });

    it('should return null for sponsor signup (public)', () => {
      expect(routeMatchers.requiresRole('/sponsor/signup')).toBeNull();
    });
  });
});
