<?php
declare(strict_types=1);

// B actual autoloaded WeekNew: SQL preview plus SELECT WHERE 1=0 only.
if ((string)getenv('POC_HEALTH_TARGET') !== 'B') {
    throw new RuntimeException('B target only');
}
require __DIR__ . '/ab_readonly_preflight.php';
$stage = 'b_week_runtime_bootstrap';
foreach ($config as $key => $value) {
    if (strpos($key, 'POC_') === 0) putenv($key . '=' . $value);
}
putenv('POC_DIALECT=mysql');
putenv('POC_EXTERNAL_STUBS=1');
define('TP_ENV', 'poc');
define('BUSINESS', 'aizhct');
define('ENTERPRISE_NO', '10001');
require $root . '/vendor/autoload.php';
(new \think\App($root))->setEnvName('poc')->initialize();
$class = \app\api\service\analysis\WeekNew::class;
$reflection = new ReflectionClass($class);
$path = $reflection->getFileName();
$expectedPath = '/Users/wiscourper/Documents/workspace/local/'
    . 'dm8-full-acceptance.fF5yTy/ai_api/app/api/service/analysis/WeekNew.php';
$expectedSha = 'bfc005287f4c185850ce92a93949e8e50e172b3290ea853692c285cb6aa78952';
if (realpath((string)$path) !== $expectedPath
    || hash_file('sha256', $expectedPath) !== $expectedSha) {
    throw new RuntimeException('B actual source path/hash mismatch');
}
$method = $reflection->getMethod('scopedMealInfos');
$method->setAccessible(true);
$service = new $class();
$stage = 'b_week_sql_preview';
$preview = $method->invoke($service)
    ->field('meal_infos.id')->whereRaw('1=0')->fetchSql(true)->select();
if (!is_string($preview)) throw new RuntimeException('SQL preview unavailable');
$sql = strtolower(str_replace(['`', '"'], '', $preview));
$aliasQualified = strpos($sql, 'meal_infos.store_id') !== false;
$physicalQualified = strpos($sql, 'yoshop_meal_infos.store_id') !== false;
if (!$aliasQualified || $physicalQualified) {
    throw new RuntimeException('B tenant alias SQL incorrect');
}
$stage = 'b_week_zero_row_execution';
$rows = $method->invoke($service)
    ->field('meal_infos.id')->whereRaw('1=0')->select()->toArray();
if (count($rows) !== 0) throw new RuntimeException('Expected zero rows');
echo json_encode(['status' => 'b-week-helper-zero-row-pass',
    'actual_autoload_sha_match' => true,
    'tenant_uses_meal_infos_alias' => true,
    'tenant_uses_physical_table_after_alias' => false,
    'zero_row_queryable' => true,
    'business_rows_read' => 0, 'writes' => 0],
    JSON_UNESCAPED_SLASHES), PHP_EOL;
