#!/usr/bin/env node
import assert from 'assert';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { ensureProjectProfile } from './src/server/helpers.js';

function mkProjectDir(name) {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), `${name}-`));
  fs.mkdirSync(path.join(dir, '.vl-code'), { recursive: true });
  fs.mkdirSync(path.join(dir, 'Theme'), { recursive: true });
  return dir;
}

function cleanup(dir) {
  fs.rmSync(dir, { recursive: true, force: true });
}

function main() {
  const seedWorkflowDir = path.join(process.cwd(), 'public', 'seed-workflows');

  const customThemeDir = mkProjectDir('vlcode-theme-custom');
  fs.writeFileSync(path.join(customThemeDir, 'Theme', 'Custom.vth'), '// VL_VERSION:3.6\n');
  ensureProjectProfile(customThemeDir, seedWorkflowDir);
  assert.equal(fs.existsSync(path.join(customThemeDir, 'Theme', 'Theme.vth')), false, 'should not seed Theme.vth when project already has a theme');
  cleanup(customThemeDir);

  const emptyThemeDir = mkProjectDir('vlcode-theme-empty');
  ensureProjectProfile(emptyThemeDir, seedWorkflowDir);
  assert.equal(fs.existsSync(path.join(emptyThemeDir, 'Theme', 'Theme.vth')), true, 'should seed Theme.vth for empty projects');
  cleanup(emptyThemeDir);

  console.log('\n── Project Profile Theme Seeding ──');
  console.log('PASS test-project-profile-theme-seeding.js');
}

main();
