/**
 * Verification test for Astro scaffold (Task 1)
 * 
 * Checks that:
 * 1. Gatsby files are removed
 * 2. Astro config files exist with correct content
 * 3. package.json has correct dependencies
 * 4. .gitignore is updated for Astro
 * 5. Build output exists in dist/
 */
import { readFileSync, existsSync, statSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const root = join(__dirname, '..');

let passed = 0;
let failed = 0;

function assert(condition, message) {
  if (condition) {
    console.log(`  ✓ ${message}`);
    passed++;
  } else {
    console.error(`  ✗ ${message}`);
    failed++;
  }
}

function readJSON(path) {
  return JSON.parse(readFileSync(path, 'utf8'));
}

function readText(path) {
  return readFileSync(path, 'utf8');
}

// === 1. Gatsby files removed ===
console.log('\n1. Gatsby files removed');
assert(!existsSync(join(root, 'gatsby-config.js')), 'gatsby-config.js removed');
assert(!existsSync(join(root, 'yarn.lock')), 'yarn.lock removed');
assert(!existsSync(join(root, 'src/gatsby-theme-blog')), 'src/gatsby-theme-blog/ removed');

// === 2. package.json correct ===
console.log('\n2. package.json');
const pkgPath = join(root, 'package.json');
assert(existsSync(pkgPath), 'package.json exists');
if (existsSync(pkgPath)) {
  const pkg = readJSON(pkgPath);
  assert(pkg.dependencies && pkg.dependencies['astro'], 'astro in dependencies');
  assert(pkg.dependencies && pkg.dependencies['@astrojs/mdx'], '@astrojs/mdx in dependencies');
  assert(pkg.dependencies && pkg.dependencies['@astrojs/rss'], '@astrojs/rss in dependencies');
  assert(pkg.devDependencies && pkg.devDependencies['typescript'], 'typescript in devDependencies');
  assert(!pkg.dependencies || !pkg.dependencies['gatsby'], 'gatsby NOT in dependencies');
  assert(pkg.scripts && pkg.scripts.build, 'build script exists');
  assert(pkg.scripts && pkg.scripts.dev, 'dev script exists');
}

// === 3. astro.config.mjs ===
console.log('\n3. astro.config.mjs');
const astroConfigPath = join(root, 'astro.config.mjs');
assert(existsSync(astroConfigPath), 'astro.config.mjs exists');
if (existsSync(astroConfigPath)) {
  const config = readText(astroConfigPath);
  assert(config.includes("site:") || config.includes("site :") || config.includes("'https://kenotron.github.io'") || config.includes('"https://kenotron.github.io"'), 'site is https://kenotron.github.io');
  assert(config.includes("base:") || config.includes("base :") || config.includes("'/crashtestdev'") || config.includes('"/crashtestdev"'), 'base is /crashtestdev');
  assert(config.includes('@astrojs/mdx'), 'mdx integration referenced');
}

// === 4. tsconfig.json ===
console.log('\n4. tsconfig.json');
const tsconfigPath = join(root, 'tsconfig.json');
assert(existsSync(tsconfigPath), 'tsconfig.json exists');
if (existsSync(tsconfigPath)) {
  const tsconfig = readJSON(tsconfigPath);
  assert(tsconfig.extends === 'astro/tsconfigs/strict', 'extends astro/tsconfigs/strict');
}

// === 5. src/env.d.ts ===
console.log('\n5. src/env.d.ts');
const envDtsPath = join(root, 'src/env.d.ts');
assert(existsSync(envDtsPath), 'src/env.d.ts exists');
if (existsSync(envDtsPath)) {
  const envDts = readText(envDtsPath);
  assert(envDts.includes('astro/client'), 'references astro/client');
}

// === 6. .gitignore updated ===
console.log('\n6. .gitignore');
const gitignorePath = join(root, '.gitignore');
assert(existsSync(gitignorePath), '.gitignore exists');
if (existsSync(gitignorePath)) {
  const gitignore = readText(gitignorePath);
  assert(gitignore.includes('dist/'), 'dist/ in .gitignore');
  assert(gitignore.includes('.astro/'), '.astro/ in .gitignore');
  assert(!gitignore.includes('# gatsby'), 'no gatsby section in .gitignore');
}

// === 7. Placeholder page ===
console.log('\n7. Placeholder page');
assert(existsSync(join(root, 'src/pages/index.astro')), 'src/pages/index.astro exists');

// === 8. Build output ===
console.log('\n8. Build output');
assert(existsSync(join(root, 'dist')), 'dist/ directory exists');

// === Summary ===
console.log(`\n${'='.repeat(40)}`);
console.log(`Results: ${passed} passed, ${failed} failed out of ${passed + failed}`);
if (failed > 0) {
  process.exit(1);
} else {
  console.log('All scaffold verification tests passed!');
}