<?php
declare(strict_types=1);

$repo = $argv[1] ?? '/workspace/zhct/zhctproject/ai_api';
$autoload = rtrim($repo, '/') . '/vendor/autoload.php';

if (!is_file($autoload)) {
    fwrite(STDERR, "autoload not found: {$autoload}" . PHP_EOL);
    exit(2);
}

require $autoload;

$ref = new ReflectionClass('app\\api\\controller\\MealCheckout');
$controller = $ref->newInstanceWithoutConstructor();
$method = $ref->getMethod('getTodayBookRuleError');
$method->setAccessible(true);

$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('+1 day'));
$rule3 = json_encode([
    'rule' => 3,
    'item' => [
        'bookingTimeSlots' => [['00:00', '23:59']],
    ],
], JSON_UNESCAPED_UNICODE);
$rule1 = json_encode(['rule' => 1], JSON_UNESCAPED_UNICODE);
$rule2 = json_encode(['rule' => 2], JSON_UNESCAPED_UNICODE);

$cases = [
    ['TC-A01', ['book_rules' => $rule3], $today, ''],
    ['TC-A02', ['book_rules' => $rule3], $tomorrow, '当日设置仅支持预订当天'],
    ['TC-A03', ['book_rules' => $rule3], 'bad-date', '就餐日期格式不正确'],
    ['TC-A04', ['book_rules' => $rule1], $tomorrow, ''],
    ['TC-A05', ['book_rules' => $rule2], $tomorrow, ''],
    ['TC-A06', ['book_rules' => ''], $tomorrow, ''],
    ['TC-A07', ['book_rules' => '{bad json'], $tomorrow, ''],
];

$failed = 0;
foreach ($cases as $case) {
    [$id, $restaurant, $mealDate, $expected] = $case;
    $actual = $method->invoke($controller, $restaurant, $mealDate);
    if ($actual !== $expected) {
        echo $id . ' FAIL expected=' . $expected . ' actual=' . $actual . PHP_EOL;
        $failed++;
        continue;
    }
    echo $id . ' PASS' . PHP_EOL;
}

if ($failed > 0) {
    echo 'FAILED (' . $failed . ' failed, ' . count($cases) . ' total)' . PHP_EOL;
    exit(1);
}

$restaurantSource = rtrim($repo, '/') . '/app/api/model/Restaurant.php';
if (!is_file($restaurantSource)) {
    fwrite(STDERR, "Restaurant source not found: {$restaurantSource}" . PHP_EOL);
    exit(2);
}
$source = file_get_contents($restaurantSource);
$hasTodayOnlyDateList = strpos($source, 'case BookRuleTypeEnum::ENUM3:') !== false
    && strpos($source, 'for ($i = 0; $i < 1; $i++)') !== false;

if (!$hasTodayOnlyDateList) {
    echo 'TC-A08 FAIL Restaurant::orderList should only generate today for today book rule' . PHP_EOL;
    exit(1);
}

$dishesSource = rtrim($repo, '/') . '/app/api/service/dishes/Dishes.php';
if (!is_file($dishesSource)) {
    fwrite(STDERR, "Dishes service source not found: {$dishesSource}" . PHP_EOL);
    exit(2);
}
$dishesServiceSource = file_get_contents($dishesSource);
$hasFutureDishesGuard = strpos($dishesServiceSource, 'isTodayBookRuleFutureDate') !== false
    && strpos($dishesServiceSource, 'emptyOrderList') !== false
    && strpos($dishesServiceSource, 'BookRuleTypeEnum::ENUM3') !== false;

if (!$hasFutureDishesGuard) {
    echo 'TC-A09 FAIL Dishes::orderList should return empty list for future date under today book rule' . PHP_EOL;
    exit(1);
}

echo 'TC-A08 PASS' . PHP_EOL;
echo 'TC-A09 PASS' . PHP_EOL;
echo 'OK (' . (count($cases) + 2) . ' cases)' . PHP_EOL;
