<?php

declare(strict_types=1);

$root = getenv('POC_APP_ROOT');
$dialect = getenv('POC_DIALECT');
$host = getenv('POC_HOST');
$port = (int)getenv('POC_PORT');
$schema = getenv('POC_SCHEMA');
$user = getenv('POC_USER');
$password = getenv('POC_PASSWORD');
if (!$root || !in_array($dialect, ['mysql', 'dm'], true) || !$host || $port < 1 || !$schema || !$user || !$password) exit(2);
define('APP_PATH', $root . '/application/');
require $root . '/thinkphp/base.php';
require $root . '/application/common.php';
if (!function_exists('config')) {
    function config($name = '', $value = null, $range = '')
    {
        return $value === null ? \think\Config::get($name) : \think\Config::set($name, $value, $range);
    }
}
\think\Config::set([
    'type' => $dialect === 'dm' ? \app\cores\database\Dm::class : 'mysql',
    'hostname' => $host, 'hostport' => $port, 'database' => $schema,
    'username' => $user, 'password' => $password,
    'prefix' => 'ydy_', 'debug' => false, 'resultset_type' => 'array',
    'fields_strict' => true, 'auto_timestamp' => false,
], 'database');
\think\Db::clear();
try {
    $context = (new \app\api\service\DishQuestionnaire(10001, 1))->context();
    echo json_encode(['dialect' => $dialect, 'status' => 'passed', 'dishes' => count($context['dishes'])]) . PHP_EOL;
} catch (\Throwable $e) {
    echo json_encode(['dialect' => $dialect, 'status' => 'failed', 'type' => get_class($e),
        'code' => (string)$e->getCode(), 'message' => substr($e->getMessage(), 0, 240)]) . PHP_EOL;
    exit(1);
}
