import { execSync } from 'node:child_process'
import { resolve } from 'node:path'
import type { ScaffoldMigrationInput } from './types.js'

export function execute(input: ScaffoldMigrationInput): { success: boolean; output: string } {
  const cmd = `dotnet ef migrations add ${input.name} --project src/${input.appCode}.Infrastructure --startup-project src/${input.appCode}.Api`
  try {
    const out = execSync(cmd, { cwd: resolve(input.projectPath), encoding: 'utf-8', timeout: 120_000 })
    return { success: true, output: out.slice(0, 2000) }
  } catch (err: any) {
    return { success: false, output: ((err.stdout ?? '') + '\n' + (err.stderr ?? '')).slice(0, 2000) }
  }
}
