/**
 * NAV-001 V2 — HubChild active-state contract.
 *
 * Cut 3 fix: `isActive` overrides `disabled` styling. When the user is
 * currently ON a disabled child's URL (e.g. `/analyze` on Day-0 with no GSC),
 * the link must render as active — "you are here" beats "you can't be here".
 */
import { render, screen } from '@testing-library/react';
import { BarChart3 } from 'lucide-react';
import { describe, expect, it } from 'vitest';

import { TooltipProvider } from '@/Components/ui/tooltip';

import { HubChild } from './HubChild';

const baseItem = {
  key: 'analyze',
  label: 'Analyze',
  icon: BarChart3,
  routeName: 'analyze.index',
  disabled: false,
};

function renderWithProvider(ui: React.ReactNode) {
  return render(<TooltipProvider>{ui}</TooltipProvider>);
}

describe('HubChild — Cut 3 active-overrides-disabled', () => {
  it('renders enabled child as a link', () => {
    renderWithProvider(
      <HubChild item={{ ...baseItem }} href="/sites/1/analyze" isActive={false} />,
    );
    const link = screen.getByTestId('hub-child-analyze');
    expect(link.tagName).toBe('A');
    expect(link).toHaveAttribute('href', '/sites/1/analyze');
  });

  it('disabled non-active child renders as a non-link span with aria-disabled', () => {
    renderWithProvider(
      <HubChild
        item={{ ...baseItem, disabled: true, disabledTooltip: 'Locked' }}
        href="/sites/1/analyze"
        isActive={false}
      />,
    );
    expect(screen.queryByTestId('hub-child-analyze')).toBeNull();
    const text = screen.getByText('Analyze');
    expect(text.parentElement).toHaveAttribute('aria-disabled', 'true');
  });

  it('disabled child that is the current URL renders as ACTIVE link (overrides disabled)', () => {
    // The contract: if the user is demonstrably on a child's URL, that child
    // must render as the active link regardless of the underlying business
    // gate. Otherwise the sidebar contradicts the URL.
    renderWithProvider(
      <HubChild
        item={{ ...baseItem, disabled: true, disabledTooltip: 'Locked' }}
        href="/sites/1/analyze"
        isActive={true}
      />,
    );
    const link = screen.getByTestId('hub-child-analyze');
    expect(link.tagName).toBe('A');
    expect(link).toHaveAttribute('aria-current', 'page');
    expect(link).toHaveAttribute('href', '/sites/1/analyze');
  });
});
