import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';

import { deriveRoiPreflightSteps } from './roi-preflight-steps';
import RoiPreflightChecklist from './RoiPreflightChecklist';

describe('RoiPreflightChecklist', () => {
  describe('deriveRoiPreflightSteps', () => {
    it('marks "Analysis complete" as done regardless of recommendations count', () => {
      expect(deriveRoiPreflightSteps(0)[0]).toMatchObject({
        label: 'Analysis complete',
        status: 'done',
      });
      expect(deriveRoiPreflightSteps(5)[0]).toMatchObject({
        label: 'Analysis complete',
        status: 'done',
      });
    });

    it('marks recommendations step as pending when known zero', () => {
      const steps = deriveRoiPreflightSteps(0);
      expect(steps[1]).toMatchObject({
        label: 'Recommendations ready',
        status: 'pending',
      });
      expect(steps[1].hint).toBeDefined();
    });

    it('marks recommendations step as done with count when > 0', () => {
      const steps = deriveRoiPreflightSteps(7);
      expect(steps[1]).toMatchObject({
        label: '7 recommendations ready',
        status: 'done',
      });
    });

    it('uses singular noun for one recommendation', () => {
      const steps = deriveRoiPreflightSteps(1);
      expect(steps[1].label).toBe('1 recommendation ready');
    });

    it('marks recommendations step as always-green when count is unknown (null)', () => {
      const steps = deriveRoiPreflightSteps(null);
      expect(steps[1]).toMatchObject({
        label: 'Recommendations ready',
        status: 'done',
      });
      // No hint when the step is green and count is unknown — the page
      // doesn't have a number to surface.
      expect(steps[1].hint).toBeUndefined();
    });

    it('always marks "Apply your first one" as pending', () => {
      expect(deriveRoiPreflightSteps(0)[2]).toMatchObject({
        label: 'Apply your first one',
        status: 'pending',
      });
      expect(deriveRoiPreflightSteps(99)[2]).toMatchObject({
        label: 'Apply your first one',
        status: 'pending',
      });
    });
  });

  describe('rendering', () => {
    it('renders the checklist as an ordered list with accessible name', () => {
      render(<RoiPreflightChecklist recommendationsReady={3} />);

      const list = screen.getByRole('list', { name: 'ROI tracking progress' });
      expect(list).toBeInTheDocument();
    });

    it('renders three list items', () => {
      render(<RoiPreflightChecklist recommendationsReady={3} />);

      expect(screen.getAllByRole('listitem')).toHaveLength(3);
    });

    it('shows "Analysis complete" in the first step', () => {
      render(<RoiPreflightChecklist recommendationsReady={0} />);

      expect(screen.getByText('Analysis complete')).toBeInTheDocument();
    });

    it('shows the recommendation count when there are recommendations', () => {
      render(<RoiPreflightChecklist recommendationsReady={4} />);

      expect(screen.getByText('4 recommendations ready')).toBeInTheDocument();
    });

    it('shows generic label when no recommendations', () => {
      render(<RoiPreflightChecklist recommendationsReady={0} />);

      expect(screen.getByText('Recommendations ready')).toBeInTheDocument();
    });

    it('shows "Apply your first one" final step', () => {
      render(<RoiPreflightChecklist recommendationsReady={3} />);

      expect(screen.getByText('Apply your first one')).toBeInTheDocument();
    });

    it('marks step 1 as done via data-status', () => {
      render(<RoiPreflightChecklist recommendationsReady={0} />);

      expect(screen.getByTestId('roi-preflight-step-1')).toHaveAttribute('data-status', 'done');
    });

    it('marks step 3 as pending via data-status', () => {
      render(<RoiPreflightChecklist recommendationsReady={3} />);

      expect(screen.getByTestId('roi-preflight-step-3')).toHaveAttribute(
        'data-status',
        'pending',
      );
    });
  });
});
