// examples/process_pipeline/main.dart
import 'package:platform_core/core.dart';
import 'package:platform_core/http.dart';
import 'package:platform_process/angel3_process.dart';
import 'package:logging/logging.dart';
import 'package:platform_container/mirrors.dart';

void main() async {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((record) {
    print('${record.level.name}: ${record.time}: ${record.message}');
  });

  // Create an Angel application with MirrorsReflector
  var app = Application(reflector: MirrorsReflector());
  var http = PlatformHttp(app);

  // Register ProcessManager as a singleton in the container
  app.container.registerSingleton(ProcessManager());

  app.get('/', (req, res) async {
    // Use dependency injection to get the ProcessManager instance
    var processManager = await req.container?.make<ProcessManager>();

    var processes = [
      angel3Process('echo', ['Hello']),
      angel3Process('sed', ['s/Hello/Greetings/']),
      angel3Process('tr', ['[:lower:]', '[:upper:]']),
    ];

    var result = await processManager?.pipeline(processes);
    res.writeln('Pipeline output: ${result?.output.trim()}');
  });

  await http.startServer('localhost', 3000);
  print('Server listening at http://localhost:3000');
}
