/**
 * R8FE-101 / R10FE-104 / ADMFE-02: Adoption guard for useMutationButton.
 *
 * Ensures the hook is imported (and therefore used) in the boundary files
 * identified by the finding — preventing regression to hand-rolled
 * useState(false) patterns.
 *
 * This is a static analysis test: it reads file contents and asserts that
 * the import is present. It catches adoption-gap regressions without
 * requiring a full component render for each file.
 *
 * R10FE-104: Extended to include R9 migration files (SiteComparisonTable,
 * Freshness/Index, TopicClusters/Index, TrafficAlerts/Index) which were
 * migrated in the R9 wave but omitted from the guard.
 *
 * ADMFE-02: Extended to include admin page migrations (GscConnections/Index,
 * CircuitBreaker) — the first Pages/Admin adopters of the hook.
 */
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

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

const ROOT = resolve(__dirname, '../../..');

const ADOPTION_FILES: { file: string; label: string }[] = [
  {
    file: 'resources/js/Pages/Batch/Show.tsx',
    label: 'Batch/Show.tsx (isRetrying → useMutationButton)',
  },
  {
    file: 'resources/js/Pages/Sites/LinkOpportunities/Index.tsx',
    label: 'Sites/LinkOpportunities/Index.tsx (isBulkUpdating/isBulkApplying/isBuilding → useMutationButton)',
  },
  {
    file: 'resources/js/Pages/OpportunityMap/Index.tsx',
    label: 'OpportunityMap/Index.tsx (runningFirstAnalysis → useMutationButton)',
  },
  {
    file: 'resources/js/Components/Recommendations/BulkActionsBar.tsx',
    label: 'BulkActionsBar.tsx (processing → useMutationButton)',
  },
  {
    file: 'resources/js/Components/Ai/DraftReviewPanel.tsx',
    label: 'DraftReviewPanel.tsx (isSubmittingRating/isRegenerating → useMutationButton)',
  },
  {
    file: 'resources/js/Pages/ContentBriefs/Index.tsx',
    label: 'ContentBriefs/Index.tsx (bulkConverting → useMutationButton)',
  },
  // R10FE-104: R9 migration files added to close the ratchet gap.
  {
    file: 'resources/js/Components/Portfolio/SiteComparisonTable.tsx',
    label: 'SiteComparisonTable.tsx (processing → useMutationButton)',
  },
  {
    file: 'resources/js/Pages/ContentIntelligence/Freshness/Index.tsx',
    label: 'ContentIntelligence/Freshness/Index.tsx (isQuickRerunSubmitting → useMutationButton)',
  },
  {
    file: 'resources/js/Pages/ContentIntelligence/TopicClusters/Index.tsx',
    label: 'ContentIntelligence/TopicClusters/Index.tsx (isRecomputing → useMutationButton)',
  },
  {
    file: 'resources/js/Pages/TrafficAlerts/Index.tsx',
    label: 'TrafficAlerts/Index.tsx (bulkAcknowledging/bulkDismissing/acknowledging/dismissing → useMutationButton)',
  },
  // ADMFE-02: first Pages/Admin adopters — GscConnections (per-row SyncButton) and CircuitBreaker (per-service ResetButton).
  {
    file: 'resources/js/Pages/Admin/GscConnections/Index.tsx',
    label: 'Admin/GscConnections/Index.tsx (syncingId useState → useMutationButton per-row SyncButton)',
  },
  {
    file: 'resources/js/Pages/Admin/CircuitBreaker.tsx',
    label: 'Admin/CircuitBreaker.tsx (resettingService useState → useMutationButton per-service ResetButton)',
  },
];

describe('useMutationButton adoption guard (R8FE-101)', () => {
  it.each(ADOPTION_FILES)('$label imports useMutationButton', ({ file }) => {
    const content = readFileSync(resolve(ROOT, file), 'utf-8');
    expect(content).toMatch(/import.*useMutationButton.*from/);
  });

  it('12+ production files import useMutationButton (ratchet: wave 1 + R9 migration + ADMFE-02 admin boundary)', () => {
    const adoptedCount = ADOPTION_FILES.filter(({ file }) => {
      const content = readFileSync(resolve(ROOT, file), 'utf-8');
      return content.includes('useMutationButton');
    }).length;
    expect(adoptedCount).toBeGreaterThanOrEqual(ADOPTION_FILES.length);
  });
});
