// Feature: client-portal, Property 3: Active tab indication
// **Validates: Requirements 3.5**

import { describe, it, expect } from 'vitest';
import * as fc from 'fast-check';
import { PORTAL_TABS, PORTAL_ROUTES, PORTAL_ANALYTICS_SUBITEMS } from '@/lib/constants/portal-navigation';

/**
 * Property: For any portal route in PORTAL_ROUTES, the PortalNav component
 * should mark exactly one tab as active, and that tab's href should match
 * the current pathname. Sub-routes under analytics are matched against
 * PORTAL_ANALYTICS_SUBITEMS.
 *
 * The active tab logic in PortalNav is: pathname === tab.href
 * We test this pure logic directly without rendering the component.
 */

/** All navigable items (tabs + analytics sub-items) */
const ALL_NAV_ITEMS = [...PORTAL_TABS, ...PORTAL_ANALYTICS_SUBITEMS.filter((item) => item.href !== '/portal/analytics')];

/**
 * Determines which nav items are active for a given pathname.
 * Mirrors the logic in PortalSidebar: isActive = pathname === item.href
 */
function getActiveItems(pathname: string) {
  return ALL_NAV_ITEMS.filter((item) => pathname === item.href);
}

/** Portal routes that have corresponding navigation items (excludes legacy detail page) */
const NAVIGABLE_PORTAL_ROUTES = PORTAL_ROUTES.filter((r) => r !== '/portal/analytics/detail');

describe('Property 3: Active tab indication', () => {
  it('for any portal route, exactly one nav item is marked as active', () => {
    fc.assert(
      fc.property(
        fc.constantFrom(...NAVIGABLE_PORTAL_ROUTES),
        (pathname) => {
          const activeItems = getActiveItems(pathname);

          // Exactly one item should be active
          expect(activeItems).toHaveLength(1);
        },
      ),
      { numRuns: 100 },
    );
  });

  it('for any portal route, the active item href matches the current pathname', () => {
    fc.assert(
      fc.property(
        fc.constantFrom(...NAVIGABLE_PORTAL_ROUTES),
        (pathname) => {
          const activeItems = getActiveItems(pathname);

          // The active item's href should equal the pathname
          expect(activeItems[0].href).toBe(pathname);
        },
      ),
      { numRuns: 100 },
    );
  });

  it('for any portal route, inactive items do not match the pathname', () => {
    fc.assert(
      fc.property(
        fc.constantFrom(...NAVIGABLE_PORTAL_ROUTES),
        (pathname) => {
          const inactiveItems = ALL_NAV_ITEMS.filter((item) => pathname !== item.href);

          // All inactive items should have hrefs different from the pathname
          for (const item of inactiveItems) {
            expect(item.href).not.toBe(pathname);
          }
        },
      ),
      { numRuns: 100 },
    );
  });

  it('every navigable PORTAL_ROUTE has a corresponding item in navigation', () => {
    fc.assert(
      fc.property(
        fc.constantFrom(...NAVIGABLE_PORTAL_ROUTES),
        (route) => {
          const matchingItem = ALL_NAV_ITEMS.find((item) => item.href === route);
          expect(matchingItem).toBeDefined();
        },
      ),
      { numRuns: 100 },
    );
  });
});
