<?php
declare(strict_types=1);
/** Pure proposal contract only: no PDO construction, network, writes or bootstrap. */
final class PlainScalarNativeContract
{
    public const FIELDS = ['name','set_uuid','use_weight','use_number','use_weight_unit','use_number_unit','use_time','use_method','employ','is_nutrition_station','supplier','nutrition_category_uuid','is_own'];
    public static function expectedSql(string $lane, array $case): string
    {
        if (!in_array($lane, ['B','C'], true) || ($case['target_DML_attempt_max'] ?? null) !== 1) {
            throw new RuntimeException('No approved reachable UPDATE shape');
        }
        $vectors = [
            'plain-scalar-overflow-string-1e999'=>['1e999','original-bound-PARAM_INT'],
            'plain-scalar-finite-over-cap-35e37'=>['3.5e38','generated-REAL'],
            'plain-scalar-positive-finish-12347'=>['12.347','generated-REAL'],
        ];
        if (($vectors[$case['label'] ?? ''] ?? null) !== [$case['value'] ?? null, $case['C_weight_shape'] ?? null]
            || ($case['php_type'] ?? null) !== 'string') throw new RuntimeException('Only exact three planned reachable vectors');
        $pairs = [];
        foreach (self::FIELDS as $field) {
            $value = ':data__'.$field;
            if ($lane === 'C' && $field === 'use_weight' && $case['C_weight_shape'] === 'generated-REAL') {
                $known = ['3.5e38'=>'CAST(3.4028234663852886e+38 AS REAL)', '12.347'=>'CAST(12.347000122070312 AS REAL)'];
                if (($known[$case['value']] ?? null) !== ($case['C_generated_expression'] ?? null)) {
                    throw new RuntimeException('Exact source-generated finite token only');
                }
                $value = $known[$case['value']];
            }
            $pairs[] = '`'.$field.'`='.$value;
        }
        return 'UPDATE `ydy_nutrition`  SET '.implode(',', $pairs).'  WHERE  `uuid` = :where_AND_uuid  ';
    }
    public static function assertUpdate(string $sql, array $binds, $executeParams, string $lane, array $case, array $params, string $ownUuid): void
    {
        if ($executeParams !== null || $sql !== self::expectedSql($lane, $case)) {
            throw new RuntimeException('Original exact SQL bytes and explicit native binds required');
        }
        $uuids = ['B'=>'B8247519-0EDA-6239-9A8A-FEA2D6C86F4B','C'=>'FCEECEFB-C32E-4977-8A65-EE2210A42095'];
        $inputKeys = ['uuid'];
        foreach (self::FIELDS as $field) if ($field !== 'is_own') $inputKeys[] = $field;
        if ($ownUuid !== $uuids[$lane] || array_keys($params) !== $inputKeys
            || ($params['uuid'] ?? null) !== $ownUuid || ($params['use_weight'] ?? null) !== $case['value'] || !is_string($params['use_weight'])
            || ($params['supplier'] ?? null) !== '康比特CDXNF11F5') {
            throw new RuntimeException('Own typed vector mismatch');
        }
        // Caller must first prove these params against the fresh full PHP typed own lease.
        $expected = [];
        foreach (self::FIELDS as $field) {
            if ($lane === 'C' && $field === 'use_weight' && $case['C_weight_shape'] === 'generated-REAL') continue;
            $value = $field === 'is_own' ? 1 : $params[$field];
            $type = in_array($field, ['use_weight','use_number','employ','is_nutrition_station','is_own'], true) ? 1 : 2;
            $expected[':data__'.$field] = ['value'=>$value,'type'=>$type,'php_type'=>gettype($value),'ok'=>true];
        }
        $expected[':where_AND_uuid'] = ['value'=>$ownUuid,'type'=>2,'php_type'=>'string','ok'=>true];
        if (array_keys($expected) !== array_keys($binds)) throw new RuntimeException('Exact original ordered bind set');
        foreach ($expected as $key=>$value) if ($binds[$key] !== $value) throw new RuntimeException('Exact original bind '.$key);
    }
    public static function assertMarker(array $plan): void
    {
        if (!array_key_exists('marker', $plan) || $plan['marker'] !== 'CDXNF11F5') throw new RuntimeException('Exact scope marker before any PDO');
        if (($plan['approval_status'] ?? '') !== 'ROOT_APPROVED_FOR_EXACT_NEW_EXECUTION') throw new RuntimeException('Proposal has no execution permission');
    }
}
