// Verification script to check consistency between frontend colors and migration
const fs = require('fs');
const path = require('path');

console.log('Verifying color consistency between frontend and backend...\n');

// Read frontend color definitions
const frontendFile = fs.readFileSync(
  path.join(__dirname, '../src/data/bambuLabColors.ts'),
  'utf8'
);

// Read migration file
const migrationFile = fs.readFileSync(
  path.join(__dirname, '../database/migrations/019_add_new_bambu_colors_2025.sql'),
  'utf8'
);

// Extract colors from frontend (looking for pattern: 'ColorName': { hex: '#HEXCODE' })
const frontendColorRegex = /'([^']+)':\s*\{\s*hex:\s*'(#[A-F0-9]{6})'/gi;
const frontendColors = {};
let match;
while ((match = frontendColorRegex.exec(frontendFile)) !== null) {
  frontendColors[match[1]] = match[2];
}

// Extract new Matte colors from migration
const migrationColorRegex = /\('([^']+)',\s*'(#[A-F0-9]{6})'\)/gi;
const migrationColors = {};
while ((match = migrationColorRegex.exec(migrationFile)) !== null) {
  migrationColors[match[1]] = match[2];
}

console.log('New colors added in migration 019:');
console.log('='.repeat(60));

let hasErrors = false;

// Check each migration color exists in frontend with same hex code
Object.keys(migrationColors).forEach(colorName => {
  const migrationHex = migrationColors[colorName];
  const frontendHex = frontendColors[colorName];

  if (!frontendHex) {
    console.log(`❌ ERROR: '${colorName}' missing in frontend`);
    hasErrors = true;
  } else if (frontendHex !== migrationHex) {
    console.log(`❌ ERROR: '${colorName}' hex mismatch:`);
    console.log(`   Frontend: ${frontendHex}`);
    console.log(`   Migration: ${migrationHex}`);
    hasErrors = true;
  } else {
    console.log(`✓ ${colorName}: ${frontendHex}`);
  }
});

// Check if PLA Wood colors exist in frontend
console.log('\nPLA Wood colors (should already exist in frontend):');
console.log('='.repeat(60));
const woodColors = ['Ochre Yellow', 'White Oak', 'Clay Brown'];
woodColors.forEach(colorName => {
  if (frontendColors[colorName]) {
    console.log(`✓ ${colorName}: ${frontendColors[colorName]}`);
  } else {
    console.log(`❌ ERROR: '${colorName}' missing in frontend`);
    hasErrors = true;
  }
});

console.log('\n' + '='.repeat(60));
if (hasErrors) {
  console.log('❌ VERIFICATION FAILED: Inconsistencies detected');
  process.exit(1);
} else {
  console.log('✓ VERIFICATION PASSED: All colors are consistent');
  process.exit(0);
}
