<?php

declare(strict_types=1);

if (($argv[1] ?? '') !== '--diagnose') {
    echo json_encode(['status' => 'default_deny', 'http' => 0, 'dml' => 0]), PHP_EOL;
    exit(78);
}
putenv('CDX_READ2_TARGET=A');
putenv('CDX_READ2_SCHEMA_PREFLIGHT=1');
require __DIR__ . '/../parallel-ai-next-read-safe4/read2_schema_preflight.php';
$journalPath = '/var/folders/21/wq30qp_s6m5f1fnbzq3vq5b80000gn/T/cdx-giftlog-CDXGL50C554EBA18E6/fixture.json';
$journal = json_decode((string)file_get_contents($journalPath), true);
if (!is_array($journal)
    || ($journal['marker'] ?? '') !== 'CDXGL50C554EBA18E6'
    || ($journal['token_states']['A'] ?? '') !== 'zero'
    || ($journal['http_states']['A'] ?? '') !== 'confirmed_failure') {
    throw new RuntimeException('Known A red journal drift');
}
$root = '/private/tmp/zhct-http-ai-macos.zjaU8h';
foreach ($config as $key => $value) {
    if (strpos($key, 'POC_') === 0) putenv($key . '=' . $value);
}
define('TP_ENV', 'poc');
define('BUSINESS', 'aizhct');
define('ENTERPRISE_NO', '10001');
require $root . '/vendor/autoload.php';
$class = new ReflectionClass(\app\api\model\user\GiftLog::class);
if ($class->getFileName()
    !== '/Users/wiscourper/Documents/workspace/local/dm8-full-acceptance.fF5yTy/baseline-a-ai-api-V4.0.40/app/api/model/user/GiftLog.php'
    || hash_file('sha256', $class->getFileName())
        !== 'cbe789c4d5d3f15ce99b3ac8a2ace58af9935d132edf70129f83cb7eb6ba2782') {
    throw new RuntimeException('Original model source drift');
}
(new \think\App($root))->setEnvName('poc')->initialize();
$ai->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$id = (int)$journal['log_ids']['owner'];
$userId = (int)$journal['owner_ids']['owner'];
$stmt = $ai->prepare('SELECT * FROM yoshop_user_gift_log'
    . ' WHERE LOG_ID=? AND USER_ID=? AND IDEMPOTENCY_KEY=? AND STORE_ID=10001');
$stmt->execute([$id, $userId, $journal['marker'] . ':O']);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($rows) !== 1) throw new RuntimeException('Original gift source not unique');
$raw = array_change_key_case($rows[0], CASE_LOWER);
$model = \app\api\model\user\GiftLog::get(['log_id' => $id, 'user_id' => $userId]);
if (!$model) throw new RuntimeException('Original gift model source absent');
$data = $model->toArray();
$first = null;
foreach (['biz_type', 'biz_order_no', 'describe', 'remark',
    'operator_id', 'operator_name', 'create_time'] as $field) {
    $actual = $data[$field] ?? null;
    $expected = $raw[$field] ?? null;
    if ((string)($actual ?? '') !== (string)($expected ?? '')) {
        $first = ['field' => $field, 'source_type' => gettype($expected),
            'model_type' => gettype($actual),
            'source_sha12' => substr(hash('sha256', (string)$expected), 0, 12),
            'model_sha12' => substr(hash('sha256', (string)$actual), 0, 12),
            'source_length' => strlen((string)$expected),
            'model_length' => strlen((string)$actual)];
        break;
    }
}
echo json_encode(['status' => 'original_model_projection_readonly',
    'first_difference' => $first, 'http' => 0, 'dml' => 0],
    JSON_UNESCAPED_SLASHES), PHP_EOL;
