<?php

declare(strict_types=1);

require __DIR__ . '/gift_contract_shape.php';

$checks = 0;
$equal = static function ($left, $right) use (&$checks): void {
    $checks++;
    if ($left !== $right) throw new RuntimeException('Gift recursive shape assertion failed');
};
$different = static function ($left, $right) use (&$checks): void {
    $checks++;
    if ($left === $right) throw new RuntimeException('Gift recursive shape change missed');
};
$listA = '{"status":200,"message":"success","trace_id":"one",'
    . '"data":{"list":{"total":1,"per_page":50,"current_page":1,'
    . '"last_page":1,"data":[{"log_id":7,"money":"-0.01",'
    . '"balance_before":"200.00","balance_after":"199.99"}]}}}';
$listB = '{"status":200,"message":"success","trace_id":"two",'
    . '"data":{"list":{"total":3,"per_page":50,"current_page":1,'
    . '"last_page":1,"data":[{"log_id":99,"money":"0.01",'
    . '"balance_before":"0.00","balance_after":"0.01"}]}}}';
$listBadPage = str_replace('"per_page":50', '"per_page":"50"', $listA);
$listLeaked = str_replace('"log_id":7', '"is_delete":0,"log_id":7', $listA);
$equal(giftLogJsonShape($listA), giftLogJsonShape($listB));
$different(giftLogJsonShape($listA), giftLogJsonShape($listBadPage));
$different(giftLogJsonShape($listA), giftLogJsonShape($listLeaked));
$detail = '{"status":200,"message":"success","trace_id":"three",'
    . '"data":{"data":{"log_id":7,"money":"-0.01",'
    . '"order_no":"TEST","pay_method":""}}}';
$detailBad = str_replace('"pay_method":""', '"pay_method":0', $detail);
$different(giftLogJsonShape($detail), giftLogJsonShape($detailBad));
$checks++;
if (!isset(giftLogJsonShape($listA)['object']['data']['object']['list']
    ['object']['per_page'])) throw new RuntimeException('Paginator type missing');
$checks++;
if (!isset(giftLogJsonShape($detail)['object']['data']['object']['data']
    ['object']['pay_method'])) throw new RuntimeException('Detail type missing');
echo 'GiftContractShapeTest pass assertions=' . $checks . PHP_EOL;
