#!/usr/bin/env node

/**
 * Build-time script to generate TypeScript file with Morgan's system prompt
 * This embeds the markdown file as a constant for Cloudflare Worker deployment
 */

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

const sourceFile = path.join(__dirname, '..', '.fortura-core', 'web-agents', 'agent-architect-web.md')
const outputFile = path.join(__dirname, '..', 'src', 'lib', 'agents', 'morgan-system-prompt.ts')

try {
  // Read the source markdown file
  const content = fs.readFileSync(sourceFile, 'utf-8')

  // Escape backticks and other special characters for TypeScript string
  const escaped = content
    .replace(/\\/g, '\\\\')  // Escape backslashes first
    .replace(/`/g, '\\`')    // Escape backticks
    .replace(/\$/g, '\\$')   // Escape dollar signs (for template literals)

  // Generate TypeScript file
  const tsContent = `/**
 * Morgan's System Prompt
 * Generated at build time from .fortura-core/web-agents/agent-architect-web.md
 * This is embedded here for Cloudflare Worker deployment compatibility
 */

export const MORGAN_SYSTEM_PROMPT = \`${escaped}\`
`

  // Write the output file
  fs.writeFileSync(outputFile, tsContent, 'utf-8')

  console.log(`✓ Generated Morgan system prompt at ${outputFile}`)
  console.log(`  Size: ${(tsContent.length / 1024).toFixed(1)} KB`)
  process.exit(0)
} catch (error) {
  console.error('✗ Failed to generate Morgan system prompt:', error.message)
  process.exit(1)
}
