import { describe, it, expect } from 'vitest';
import * as fs from 'fs/promises';
import * as path from 'path';
import { glob } from 'glob';

describe('Website boundary: pages/components must not import mappers or generated DTOs', () => {
  it('rejects imports from mappers and types/generated', async () => {
    const websiteRoot = path.resolve(__dirname, '../../..');

    const candidates = await glob([
      'app/leagues/[id]/schedule/**/*.{ts,tsx}',
      'components/leagues/LeagueSchedule.tsx',
    ], {
      cwd: websiteRoot,
      absolute: true,
      nodir: true,
      ignore: ['**/*.test.*', '**/*.spec.*'],
    });

    const forbiddenImportRegex =
      /^\s*import[\s\S]*from\s+['"][^'"]*(\/mappers\/|\/types\/generated\/)[^'"]*['"]/gm;

    const offenders: Array<{ file: string; matches: string[] }> = [];

    for (const file of candidates) {
      const content = await fs.readFile(file, 'utf-8');

      const matches = Array.from(content.matchAll(forbiddenImportRegex)).map((m) => m[0]).filter(Boolean);

      if (matches.length > 0) {
        offenders.push({
          file: path.relative(websiteRoot, file),
          matches,
        });
      }
    }

    expect(offenders, `Forbidden imports found:\n${offenders
      .map((o) => `- ${o.file}\n${o.matches.map((m) => `  ${m}`).join('\n')}`)
      .join('\n')}`).toEqual([]);
  });
});