/**
 * F4GTM-006: Competitor data staleness guard.
 *
 * Asserts:
 * - No competitor's `lastVerified` is more than 90 days old relative to today
 *   (stale pricing/feature data harms conversion and E-E-A-T credibility)
 * - Slug parity: every slug in TS competitors matches the PHP config/competitors.php
 *   (the PHP slugs are the source of truth for routing and sitemap; TS must stay in sync)
 */

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

import { competitors } from './competitors';

/** The 22 slugs from config/competitors.php (source of truth for routing + sitemap). */
const PHP_COMPETITOR_SLUGS = [
  'surfer-seo',
  'clearscope',
  'rank-math',
  'yoast-seo',
  'ahrefs',
  'semrush',
  'marketmuse',
  'frase',
  'neuronwriter',
  'page-optimizer-pro',
  'se-ranking',
  'mangools',
  'moz',
  'ubersuggest',
  'serpstat',
  'writerzen',
  'jasper',
  'google-search-console',
  'searchatlas-otto',
  'seotesting',
  'rankiq',
  'koala-ai',
] as const;

const STALENESS_THRESHOLD_DAYS = 90;

describe('competitor data freshness and slug parity (F4GTM-006)', () => {
  it('no competitor lastVerified is more than 90 days old', () => {
    const today = new Date();
    const stale: string[] = [];

    for (const [slug, config] of Object.entries(competitors)) {
      const verified = new Date(config.lastVerified);
      const diffMs = today.getTime() - verified.getTime();
      const diffDays = diffMs / (1000 * 60 * 60 * 24);

      if (diffDays > STALENESS_THRESHOLD_DAYS) {
        stale.push(
          `${slug}: lastVerified="${config.lastVerified}" is ${Math.floor(diffDays)} days old (max ${STALENESS_THRESHOLD_DAYS})`,
        );
      }
    }

    expect(
      stale,
      `Stale competitor data detected — update lastVerified in resources/js/data/competitors.ts:\n${stale.join('\n')}`,
    ).toHaveLength(0);
  });

  it('every PHP competitor slug exists in the TS competitors record', () => {
    const tsKeys = new Set(Object.keys(competitors));
    const missing: string[] = [];

    for (const slug of PHP_COMPETITOR_SLUGS) {
      if (!tsKeys.has(slug)) {
        missing.push(slug);
      }
    }

    expect(
      missing,
      `These slugs are in config/competitors.php but missing from resources/js/data/competitors.ts: ${missing.join(', ')}`,
    ).toHaveLength(0);
  });

  it('every TS competitor slug exists in the PHP competitors config', () => {
    const phpSlugs = new Set(PHP_COMPETITOR_SLUGS);
    const extras: string[] = [];

    for (const slug of Object.keys(competitors)) {
      if (!phpSlugs.has(slug as (typeof PHP_COMPETITOR_SLUGS)[number])) {
        extras.push(slug);
      }
    }

    expect(
      extras,
      `These slugs are in resources/js/data/competitors.ts but missing from config/competitors.php: ${extras.join(', ')}`,
    ).toHaveLength(0);
  });

  it('all lastVerified dates are valid ISO 8601 (YYYY-MM-DD) format', () => {
    const isoPattern = /^\d{4}-\d{2}-\d{2}$/;
    const invalid: string[] = [];

    for (const [slug, config] of Object.entries(competitors)) {
      if (!isoPattern.test(config.lastVerified)) {
        invalid.push(`${slug}: "${config.lastVerified}"`);
      }
    }

    expect(
      invalid,
      `Invalid lastVerified date format (expected YYYY-MM-DD):\n${invalid.join('\n')}`,
    ).toHaveLength(0);
  });
});
