/**
 * SEO2-004: Nav/footer competitor lists must be a single source of truth.
 *
 * Asserts:
 * - COMPETITOR_NAV_LIST is derived from data/competitors.ts (no orphan slugs)
 * - COMPETITOR_NAV_LIST is sorted by navPriority (ascending)
 * - Footer slice(0,6) matches the 6 required high-priority entries
 * - All 22 competitors are present
 */

import { describe, it, expect } from 'vitest';

import { competitors, COMPETITOR_NAV_LIST } from './competitors';

describe('COMPETITOR_NAV_LIST (SEO2-004)', () => {
  it('every slug in COMPETITOR_NAV_LIST exists in the competitors record', () => {
    const knownSlugs = new Set(Object.keys(competitors));
    for (const entry of COMPETITOR_NAV_LIST) {
      expect(knownSlugs.has(entry.slug), `Slug "${entry.slug}" not found in competitors record`).toBe(true);
    }
  });

  it('COMPETITOR_NAV_LIST contains all 22 competitors', () => {
    expect(COMPETITOR_NAV_LIST).toHaveLength(22);
  });

  it('COMPETITOR_NAV_LIST is sorted by navPriority ascending', () => {
    const priorities = COMPETITOR_NAV_LIST.map((c) => competitors[c.slug].navPriority);
    for (let i = 1; i < priorities.length; i++) {
      expect(priorities[i]).toBeGreaterThanOrEqual(priorities[i - 1]);
    }
  });

  it('footer slice(0, 6) surfaces the 6 required priority entries in correct order', () => {
    const footerSlugs = COMPETITOR_NAV_LIST.slice(0, 6).map((c) => c.slug);
    expect(footerSlugs).toEqual([
      'surfer-seo',
      'searchatlas-otto',
      'rankiq',
      'seotesting',
      'clearscope',
      'semrush',
    ]);
  });

  it('every competitor has a navPriority field', () => {
    for (const [slug, config] of Object.entries(competitors)) {
      expect(
        typeof config.navPriority,
        `competitors["${slug}"].navPriority must be a number`,
      ).toBe('number');
    }
  });

  it('every competitor has a publishedAt field in YYYY-MM-DD format', () => {
    const isoPattern = /^\d{4}-\d{2}-\d{2}$/;
    for (const [slug, config] of Object.entries(competitors)) {
      expect(
        isoPattern.test(config.publishedAt),
        `competitors["${slug}"].publishedAt must be YYYY-MM-DD, got "${config.publishedAt}"`,
      ).toBe(true);
    }
  });

  it('label format is "vs <Name>" for every entry', () => {
    for (const entry of COMPETITOR_NAV_LIST) {
      expect(entry.label, `Expected label to start with "vs " for slug "${entry.slug}"`).toMatch(
        /^vs /,
      );
    }
  });
});
