import fs from 'node:fs';
import path from 'node:path';

const root = process.cwd();
const files = [
  'src/TaxBaik.Web/Program.cs',
  'src/TaxBaik.Web.Client/Components/Admin/App.razor',
  'src/TaxBaik.Web.Client/Components/Admin/Routes.razor',
  'src/TaxBaik.Web.Client/Components/Admin/RedirectToLogin.razor',
  'tests/e2e/public-smoke.spec.ts',
  'tests/e2e/admin-smoke.spec.ts',
  'tests/e2e/route-isolation.spec.ts'
];

const required = [
  {
    file: 'src/TaxBaik.Web/Program.cs',
    pattern: 'app.MapRazorPages();',
    message: '관리자 화면은 Razor Pages 라우팅을 사용해야 합니다.'
  },
  {
    file: 'tests/e2e/route-isolation.spec.ts',
    pattern: 'public home does not boot the admin app',
    message: '공용/관리자 분리 하네스가 필요합니다.'
  },
  {
    file: 'tests/e2e/route-isolation.spec.ts',
    pattern: 'admin login boots the admin app under the admin base path',
    message: '관리자 base path 하네스가 필요합니다.'
  },
  {
    file: 'tests/e2e/admin-smoke.spec.ts',
    pattern: '.admin-shell',
    message: '관리자 셸 렌더 검증이 필요합니다.'
  }
];

let failed = false;

for (const entry of required) {
  const fullPath = path.join(root, entry.file);
  const text = fs.readFileSync(fullPath, 'utf8');
  if (!text.includes(entry.pattern)) {
    console.error(`[route-lint] FAIL ${entry.file}: missing "${entry.pattern}"`);
    console.error(`  ${entry.message}`);
    failed = true;
  }
}

const adminFiles = files
  .filter(file => file.includes('/Admin/') || file.includes('admin-smoke') || file.includes('route-isolation'));

for (const file of adminFiles) {
  const fullPath = path.join(root, file);
  const text = fs.readFileSync(fullPath, 'utf8');

  if (text.includes('ToBaseRelativePath(Navigation.Uri)')) {
    console.error(`[route-lint] FAIL ${file}: unsafe ToBaseRelativePath(Navigation.Uri) usage detected.`);
    failed = true;
  }

  if (file === 'src/TaxBaik.Web.Client/Components/Admin/App.razor') {
    if (text.includes('<html') || text.includes('<body') || text.includes('blazor.webassembly.js')) {
      console.error('[route-lint] FAIL App.razor: host HTML or Blazor bootstrap script must not live in the root component.');
      failed = true;
    }
  }

  if (file === 'src/TaxBaik.Web.Client/Components/Admin/Layout/MainLayout.razor') {
    if (!text.includes('<AdminShell>')) {
      console.error('[route-lint] FAIL MainLayout: admin shell wrapper should be restored.');
      failed = true;
    }
  }
}

if (failed) process.exit(1);
console.log('[route-lint] OK');
