/**
 * Pitch Video Render Script
 *
 * Programmatic rendering of pitch videos via Remotion API.
 * Usage: pnpm render:pitch:api [script.json] [output.mp4]
 */

import { bundle } from '@remotion/bundler';
import { renderMedia, selectComposition } from '@remotion/renderer';
import path from 'path';
import fs from 'fs';
import { stemedbPitchScript } from './data/stemedb-pitch';
import { calculateTotalDuration } from './types/pitch-script';
import type { PitchScript } from './types/pitch-script';

async function main() {
  const args = process.argv.slice(2);
  const scriptPath = args[0];
  const outputPath = args[1] || 'out/stemedb-pitch.mp4';

  // Load script from file or use default
  let script: PitchScript;
  if (scriptPath && fs.existsSync(scriptPath)) {
    console.log(`Loading script from ${scriptPath}`);
    script = JSON.parse(fs.readFileSync(scriptPath, 'utf-8'));
  } else {
    console.log('Using default StemeDB pitch script');
    script = stemedbPitchScript;
  }

  // Calculate total duration
  const totalDuration = calculateTotalDuration(script.blocks);
  console.log(`Total duration: ${(totalDuration / 1000).toFixed(1)}s (${script.blocks.length} blocks)`);

  // Bundle the Remotion project
  console.log('Bundling...');
  const bundleLocation = await bundle({
    entryPoint: path.resolve(__dirname, './index.ts'),
    webpackOverride: (config) => config,
  });

  // Select the composition
  const composition = await selectComposition({
    serveUrl: bundleLocation,
    id: 'PitchVideo',
    inputProps: { script },
  });

  // Ensure output directory exists
  const outputDir = path.dirname(outputPath);
  if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir, { recursive: true });
  }

  // Render the video
  console.log(`Rendering to ${outputPath}...`);
  await renderMedia({
    composition,
    serveUrl: bundleLocation,
    codec: 'h264',
    outputLocation: outputPath,
    inputProps: { script },
    onProgress: ({ progress }) => {
      const percent = Math.round(progress * 100);
      process.stdout.write(`\rProgress: ${percent}%`);
    },
  });

  console.log('\nDone!');
  console.log(`Output: ${path.resolve(outputPath)}`);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
