#!/usr/bin/env tsx
import { applyUpdate, previewUpdate } from '../skills-engine/update.js';

const args = process.argv.slice(2);
const jsonMode = args.includes('--json');
const previewOnly = args.includes('--preview-only');
const newCorePath = args.find((a) => !a.startsWith('--'));

if (!newCorePath) {
  console.error(
    'Usage: tsx scripts/update-core.ts [--json] [--preview-only] <path-to-new-core>',
  );
  process.exit(1);
}

// Preview
const preview = previewUpdate(newCorePath);

if (jsonMode && previewOnly) {
  console.log(JSON.stringify(preview, null, 2));
  process.exit(0);
}

function printPreview(): void {
  console.log('=== Update Preview ===');
  console.log(`Current version: ${preview.currentVersion}`);
  console.log(`New version:     ${preview.newVersion}`);
  console.log(`Files changed:   ${preview.filesChanged.length}`);
  if (preview.filesChanged.length > 0) {
    for (const f of preview.filesChanged) {
      console.log(`  ${f}`);
    }
  }
  if (preview.conflictRisk.length > 0) {
    console.log(`Conflict risk:   ${preview.conflictRisk.join(', ')}`);
  }
  if (preview.customPatchesAtRisk.length > 0) {
    console.log(
      `Custom patches at risk: ${preview.customPatchesAtRisk.join(', ')}`,
    );
  }
}

if (previewOnly) {
  printPreview();
  process.exit(0);
}

if (!jsonMode) {
  printPreview();
  console.log('');
  console.log('Applying update...');
}

const result = await applyUpdate(newCorePath);

console.log(JSON.stringify(result, null, 2));

if (!result.success) {
  process.exit(1);
}
