<?php
// Isolated execution of the actual scoring loop and ingredient selector; no app boot or DB.
set_error_handler(function ($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
});
class DishesFoods {
    public static $rows = [];
    public static function getAllData($where, $field) { return self::$rows; }
}
class FoodsModel {
    public static $food;
    public static function getOne($where) { return self::$food; }
}
$source = file_get_contents($argv[1]);
$start = strpos($source, '        foreach ($current_meal as $item){');
$end = strpos($source, '        $cooking_menthod_ratio', $start);
$methodStart = strpos($source, '    public function getRatioFoods(');
$methodEnd = strpos($source, '    public function getCategoryScore(', $methodStart);
if ($start === false || $end === false || $methodStart === false || $methodEnd === false) {
    throw new RuntimeException('Source anchors missing');
}
$loop = substr($source, $start, $end - $start);
$selector = substr($source, $methodStart, $methodEnd - $methodStart);
eval('class ScoringUnderTest {' . $selector . '
    public function score($current_meal) {
        $dishes_cate = [1 => ["count" => 0]];
        $cooking_menthod = [1 => ["recommend" => 1]];
        $cooking_menthod_count = 0;
        $czl_score = $sl_score = $sssc_score = $sg_score = $nl_score = $dl_score = $jg_score = $hcp_score = 0;
        ' . $loop . '
        return [$dl_score, $hcp_score, $sssc_score, $cooking_menthod_count];
    }
}');
$validRows = [['foods_id' => 11, 'foods_weight' => 20]];
$cases = [
    ['empty ingredients bean branch', [], [], 1, [0,0,0,1]],
    ['empty ingredients vegetable branch', [], [], 9, [0,0,0,1]],
    ['empty ingredients seafood branch', [], [], 10, [0,0,0,1]],
    ['zero weight', [['foods_id'=>11,'foods_weight'=>0]], [], 9, [0,0,0,1]],
    ['negative weight', [['foods_id'=>11,'foods_weight'=>-1]], [], 9, [0,0,0,1]],
    ['missing ingredient id', [['foods_weight'=>10]], [], 9, [0,0,0,1]],
    ['missing weight', [['foods_id'=>11]], [], 9, [0,0,0,1]],
    ['orphan ingredient', $validRows, null, 9, [0,0,0,1]],
    ['missing category', $validRows, new ArrayObject([]), 9, [0,0,0,1]],
    ['beans', $validRows, new ArrayObject(['category'=>12,'category_two'=>0]), 9, [2,0,0,1]],
    ['seafood', $validRows, new ArrayObject(['category'=>5,'category_two'=>0]), 10, [0,2,0,1]],
    ['dark vegetable', $validRows, new ArrayObject(['category'=>1,'category_two'=>38]), 7, [0,0,2,1]],
    ['ordinary ingredient', $validRows, new ArrayObject(['category'=>1,'category_two'=>1]), 9, [0,0,0,1]],
];
$failures = 0;
foreach ($cases as $case) {
    list($name, $rows, $food, $dishType, $expected) = $case;
    DishesFoods::$rows = $rows;
    FoodsModel::$food = $food;
    try {
        $actual = (new ScoringUnderTest())->score([['category'=>1,'dishe'=>['id'=>1,'category_two'=>$dishType,'cook_method'=>1]]]);
        if ($actual !== $expected) { throw new RuntimeException(json_encode($actual)); }
        echo "PASS $name\n";
    } catch (Throwable $e) {
        $failures++;
        echo "FAIL $name: " . $e->getMessage() . "\n";
    }
}
echo count($cases) . " cases, $failures failures\n";
exit($failures ? 1 : 0);
