#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

console.log('🧪 Testing build locally...\n');

// Clean previous build
console.log('🧹 Cleaning previous build...');
try {
  execSync('rm -rf .next', { stdio: 'inherit' });
} catch (e) {
  // Ignore if doesn't exist
}

// Run build
console.log('\n🔨 Running build...');
try {
  execSync('npm run build', { stdio: 'inherit' });
} catch (e) {
  console.error('❌ Build failed!');
  process.exit(1);
}

// Check for required files
console.log('\n🔍 Checking build output...');
const checks = [
  { path: 'out/index.html', name: 'Static HTML output' },
  { path: '.next/static', name: 'Static directory' },
];

let allPassed = true;
for (const check of checks) {
  if (fs.existsSync(check.path)) {
    console.log(`✅ ${check.name}: Found`);
  } else {
    console.error(`❌ ${check.name}: Missing at ${check.path}`);
    allPassed = false;
  }
}

if (!allPassed) {
  console.error('\n❌ Build test failed! Do not push.');
  process.exit(1);
} else {
  console.log('\n✅ All build tests passed!');
}