import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fail, pass, repositoryRoot } from './lib.mjs';

const errors = [];
const metadata = await readFile(
  path.join(repositoryRoot, 'src', 'lib', 'metadata', 'build-metadata.ts'),
  'utf8',
);
const homepage = await readFile(
  path.join(repositoryRoot, 'src', 'app', '[locale]', 'page.tsx'),
  'utf8',
);
const pending = await readFile(
  path.join(repositoryRoot, 'src', 'app', '[locale]', '[slug]', 'page.tsx'),
  'utf8',
);

for (const required of [
  'alternates',
  'canonical',
  'hreflangMap',
  'openGraph',
  'alternateLocale',
  'twitter',
  'robots',
]) {
  if (!metadata.includes(required)) errors.push(`Metadata foundation omits ${required}.`);
}
if (!homepage.includes("routeId: 'home'")) errors.push('Homepage metadata is not route-ID driven.');
if (!pending.includes('indexable: false')) {
  errors.push('Unapproved route shells must remain noindex.');
}
if (/application\/ld\+json|aggregateRating|priceCurrency/.test(metadata + homepage + pending)) {
  errors.push('Unsupported structured data or pricing metadata was introduced.');
}

if (errors.length) fail(errors);
else pass('Localized canonical, hreflang, Open Graph, social, and robots foundations are present.');
