#!/usr/bin/env node
/**
 * CLI Test Runner for Content Generation
 * Usage: node scripts/test-campaign.js
 */

import { pool } from '../src/lib/db.js';
import { batchQueue } from '../src/lib/queue/config.js';

const testBlueprint = {
  "asset_name": "{{CITY}} Test Campaign",
  "deployment_target": "Test Funnel",
  "variables": {
    "STATE": "California",
    "CITY": "San Diego|Irvine",
    "AVATAR_A": "Solar CEO"
  },
  "content": {
    "url_path": "{{CITY}}.test.com",
    "meta_description": "Test campaign for {{CITY}}",
    "body": [
      {
        "block_type": "Hero",
        "content": "<h1>{Welcome|Hello} to {{CITY}}</h1><p>This is a {test|demo} for {{AVATAR_A}}s.</p>"
      }
    ]
  }
};

async function runTest() {
  console.log('🧪 Content Generation Test\n');

  try {
    // 1. Get site ID
    const siteResult = await pool.query(
      `SELECT id FROM sites WHERE domain = 'spark.jumpstartscaling.com' LIMIT 1`
    );
    
    if (siteResult.rows.length === 0) {
      throw new Error('Admin site not found');
    }
    
    const siteId = siteResult.rows[0].id;
    console.log(`✓ Site ID: ${siteId}`);

    // 2. Create campaign
    const campaignResult = await pool.query(
      `INSERT INTO campaign_masters (site_id, name, blueprint_json, status)
       VALUES ($1, $2, $3, 'pending')
       RETURNING id`,
      [siteId, 'Test Campaign', JSON.stringify(testBlueprint)]
    );

    const campaignId = campaignResult.rows[0].id;
    console.log(`✓ Campaign created: ${campaignId}`);

    // 3. Queue job
    await batchQueue.add('generate_campaign_content', {
      campaignId,
      campaignName: 'Test Campaign'
    });
    
    console.log(`✓ Job queued for campaign ${campaignId}`);
    console.log('\n📊 Expected output: 2 posts (San Diego, Irvine)');
    console.log('🔍 Check generation_jobs table for status');

  } catch (error) {
    console.error('❌ Test failed:', error.message);
    process.exit(1);
  } finally {
    await pool.end();
    process.exit(0);
  }
}

runTest();
