#!/usr/bin/env node

/**
 * start CLI — Start a new feature/release/hotfix branch
 */

import { parseArgs } from 'node:util';
import { validate } from './validate.js';
import { execute } from './execute.js';
import { StartResultSchema } from './types.js';

async function main() {
  try {
    const args = parseArgs({
      options: {
        spec: { type: 'string' },
        json: { type: 'boolean', default: false },
      },
      allowPositionals: false,
    });

    if (!args.values.spec) {
      console.error('Error: --spec is required');
      process.exit(2);
    }

    let specObj: unknown;
    try {
      specObj = JSON.parse(args.values.spec);
    } catch {
      console.error('Error: --spec must be valid JSON');
      process.exit(2);
    }

    const validation = validate(specObj);
    if (!validation.valid) {
      if (args.values.json) {
        console.log(JSON.stringify({
          success: false,
          error: validation.blockers.join('; '),
        }, null, 2));
      } else {
        console.error('Validation failed:');
        validation.blockers.forEach(b => console.error(`  - ${b}`));
      }
      process.exit(2);
    }

    const result = await execute(validation.data!);
    const validated = StartResultSchema.parse(result);

    if (args.values.json) {
      console.log(JSON.stringify(validated, null, 2));
    } else {
      if (validated.success) {
        console.log(`✓ Branch ${validated.branch} created`);
        if (validated.worktreePath) {
          console.log(`  Worktree: ${validated.worktreePath}`);
        }
        if (validated.pushed) {
          console.log(`  Pushed to origin`);
        }
      } else {
        console.error(`✗ ${validated.error}`);
      }
      for (const w of validated.warnings ?? []) {
        console.warn(`⚠ ${w}`);
      }
    }

    process.exit(validated.success ? 0 : 1);
  } catch (err: unknown) {
    console.error('Fatal error:', (err as Error).message);
    process.exit(2);
  }
}

main();
