<?php
declare(strict_types=1);
/** Metadata-only contract. Never connects to a database or reads business rows. */
function giftContract(): array
{
    $b=json_decode(file_get_contents(__DIR__.'/metadata_B.json'),true);$run=json_decode(file_get_contents(__DIR__.'/metadata_C_run.json'),true);$c=$run['report'];
    if($b['status']!=='pass'||$c['status']!=='pass'||$run['exit_code']!==0||$run['stderr_bytes']!==0||$b['table']!=='yoshop_user_gift_log'||$c['table']!==$b['table']){throw new RuntimeException('Frozen metadata gate');}
    $source=array_column($b['columns'],null,'name');$target=array_column($c['columns'],null,'name');
    if(count($source)!==16||count($target)!==19||$source['biz_order_id']['source_column_type']!=='bigint(20) unsigned'||$target['biz_order_id']['native_type']!=='decimal'||$target['biz_order_id']['precision']!==20||$target['biz_order_id']['scale']!==0){throw new RuntimeException('Unsigned source/target precision gate');}
    $cols=[];foreach($c['columns'] as $col){$name=$col['name'];$rule=null;$type=$col['native_type'];
        if(in_array($name,['money','balance_before','balance_after'],true)){
            if($source[$name]['native_type']!=='decimal'||$source[$name]['precision']!==10||$source[$name]['scale']!==2||$type!=='decimal'||$col['precision']!==10||$col['scale']!==2){throw new RuntimeException('Money schema gate');}
            $rule=['kind'=>'decimal_text','precision'=>10,'scale'=>2];
        }elseif($name==='biz_order_id'){$rule=['kind'=>'unsigned_integer','precision'=>0,'scale'=>0];}
        if($type==='decimal'){$type.='('.$col['precision'].','.$col['scale'].')';}
        $cols[]=['ordinal'=>$col['ordinal'],'name'=>$name,'native_type'=>$type,'rule'=>$rule,'source_present'=>isset($source[$name])];
    }
    return ['version'=>'gift-columns-20261010-v1','table'=>'yoshop_user_gift_log','source'=>'approved local MySQL56 and nonproduction DM5238 metadata only','source_metadata_sha256'=>hash_file('sha256',__DIR__.'/metadata_B.json'),'target_metadata_sha256'=>hash_file('sha256',__DIR__.'/metadata_C_run.json'),'star_allowed'=>false,'columns'=>$cols,'limitations'=>['target_extra_columns_and_order','create_time_integer_vs_datetime','source_unsigned_log_id_vs_target_signed_bigint']];
}
