#!/var/task/native/bref/bin/php
<?php declare(strict_types=1);

use Bref\Runtime\LambdaRuntime;
use Bref\Runtime\PhpFpm;

ini_set('display_errors', '1');
error_reporting(E_ALL);

$appRoot = getenv('LAMBDA_TASK_ROOT');

require $appRoot . '/vendor/autoload.php';

$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable();

$handler = $appRoot . '/' . getenv('_HANDLER');
if (! is_file($handler)) {
    $lambdaRuntime->failInitialization("Handler `$handler` doesn't exist");
}

$phpFpm = new PhpFpm($handler, '/var/task/native/bref/etc/php-fpm.conf');
try {
    $phpFpm->start();
} catch (\Throwable $e) {
    $lambdaRuntime->failInitialization('Error while starting PHP-FPM', $e);
}

while (true) {
    $lambdaRuntime->processNextEvent(function ($event) use ($phpFpm): array {
        if (isset($event['body'])) {
            $body = @json_decode($event['body'], true);

            if (isset($body['method']) && ! isset($event['httpMethod'])) {
                $event['httpMethod'] = $body['method'];
            }
        }

        $response = $phpFpm->proxy($event);

        $multiHeader = array_key_exists('multiValueHeaders', $event);
        return $response->toApiGatewayFormat($multiHeader);
    });

    try {
        $phpFpm->ensureStillRunning();
    } catch (\Throwable $e) {
        echo $e->getMessage();
        exit(1);
    }
}
