#!/usr/bin/env node

/**
 * Sync version across all plugin files
 * 
 * This script can work in two modes:
 * 1. Sync FROM package.json TO all plugin files (default)
 * 2. Sync TO package.json FROM a specified version (--to-package flag)
 * 
 * Usage:
 *   node sync-version.js                    # Sync FROM package.json to all files
 *   node sync-version.js --from VERSION     # Update package.json to VERSION, then sync to all files
 */

const fs = require('fs');
const path = require('path');

// Parse command line arguments
const args = process.argv.slice(2);
const fromVersionIndex = args.indexOf('--from');
const targetVersion = fromVersionIndex >= 0 ? args[fromVersionIndex + 1] : null;

let version;

if (targetVersion) {
  // Mode 2: Update package.json first, then sync to all files
  console.log(`Setting version to ${targetVersion} across all files...`);
  
  // Update package.json
  const packageJsonPath = path.join(__dirname, '..', 'package.json');
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
  packageJson.version = targetVersion;
  fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
  console.log(`✓ Updated package.json to version ${targetVersion}`);
  
  version = targetVersion;
} else {
  // Mode 1: Read version from package.json
  const packageJsonPath = path.join(__dirname, '..', 'package.json');
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
  version = packageJson.version;
  console.log(`Syncing version ${version} from package.json to all plugin files...`);
}

// Update recast-paywall.php
const mainPluginFile = path.join(__dirname, '..', 'recast-paywall.php');
let phpContent = fs.readFileSync(mainPluginFile, 'utf8');

// Update Version in plugin header
phpContent = phpContent.replace(
  /(\* Version:\s+)[\d.]+/,
  `$1${version}`
);

// Update Stable tag in plugin header
phpContent = phpContent.replace(
  /(\* Stable tag:\s+)[\d.]+/,
  `$1${version}`
);

// Update RECAST_PAYWALL_VERSION constant
phpContent = phpContent.replace(
  /(define\('RECAST_PAYWALL_VERSION',\s+')[\d.]+('\);)/,
  `$1${version}$2`
);

fs.writeFileSync(mainPluginFile, phpContent);
console.log(`✓ Updated recast-paywall.php`);

// Update readme.txt
const readmePath = path.join(__dirname, '..', 'readme.txt');
let readmeContent = fs.readFileSync(readmePath, 'utf8');

// Update Stable tag
readmeContent = readmeContent.replace(
  /(Stable tag:\s+)[\d.]+/,
  `$1${version}`
);

fs.writeFileSync(readmePath, readmeContent);
console.log(`✓ Updated readme.txt`);

// Update composer.json if it exists and has a version field
const composerPath = path.join(__dirname, '..', 'composer.json');
if (fs.existsSync(composerPath)) {
  let composerContent = fs.readFileSync(composerPath, 'utf8');
  
  // Only update if version field exists
  if (composerContent.includes('"version"')) {
    composerContent = composerContent.replace(
      /("version":\s*")[\d.]+(")/,
      `$1${version}$2`
    );
    fs.writeFileSync(composerPath, composerContent);
    console.log(`✓ Updated composer.json`);
  }
}

console.log(`\n✅ Version sync complete! All files updated to version ${version}`);
