import { config } from '../../config.js';
import { detectProject } from '../../lib/frameworks/detect.js';
import { LaravelAdapter } from '../../lib/frameworks/laravel.js';
import type { FrameworkAdapter } from '../../lib/frameworks/types.js';

export function requireLaravel(): LaravelAdapter {
  const detected = detectProject(config.workspaceRoot);
  if (detected.backend instanceof LaravelAdapter) {
    return detected.backend;
  }
  throw new Error(
    'No Laravel project detected in WORKSPACE_ROOT. ' +
      'Expected composer.json with laravel/framework (or an artisan file) at the workspace root.',
  );
}

/** Any Composer-based PHP backend (Laravel or CodeIgniter). */
export function requirePhpBackend(): FrameworkAdapter {
  const detected = detectProject(config.workspaceRoot);
  if (detected.backend && (detected.backend.kind === 'laravel' || detected.backend.kind === 'codeigniter')) {
    return detected.backend;
  }
  throw new Error(
    'No PHP backend (Laravel or CodeIgniter) detected in WORKSPACE_ROOT. ' +
      'Expected a composer.json with laravel/framework or codeigniter4/framework.',
  );
}
