#!/usr/bin/env node
/**
 * Export default workflows as JSON for the backend API
 * Run: node packages/core/scripts/export-default-workflows.js
 */

import { writeFileSync, mkdirSync, existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { WorkflowGraph } from '../src/framework/graph.js';
import { buildAnalysisGraph } from '../templates/graphs/analysisGraph.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const outputDir = join(__dirname, '../../../backend/src/data');

// Ensure output directory exists
if (!existsSync(outputDir)) {
  mkdirSync(outputDir, { recursive: true });
}

// Build and serialize analysis workflow
console.log('Building analysis workflow...');
const analysisGraph = new WorkflowGraph();
buildAnalysisGraph(analysisGraph);
const serializedAnalysis = analysisGraph.serialize();

// Add workflow metadata
const analysisWorkflow = {
  id: 'default_analysis',
  name: 'Analysis Workflow',
  description: 'Analyzes tickets and generates code implementation',
  ...serializedAnalysis,
  createdAt: new Date().toISOString(),
  isDefault: true
};

// Write to JSON file
const outputPath = join(outputDir, 'default-workflows.json');
const workflows = {
  analysis: analysisWorkflow
};

writeFileSync(outputPath, JSON.stringify(workflows, null, 2));
console.log(`✅ Exported default workflows to ${outputPath}`);

// Also export as individual files for clarity
writeFileSync(
  join(outputDir, 'default-analysis-workflow.json'),
  JSON.stringify(analysisWorkflow, null, 2)
);
console.log('✅ Exported individual workflow files');
