<?php

declare(strict_types=1);

require '/workspace/zhct/zhctproject/store/tests/bootstrap.php';
require_once '/workspace/zhct/zhctproject/store/thinkphp/helper.php';

\think\Config::set(require APP_PATH . 'database.php', 'database');
$configured = \think\Config::get('database');
if (TP_ENV !== 'local'
    || $configured['ai_zhct']['hostname'] !== '101.200.165.56'
    || $configured['ai_zhct']['database'] !== 'ai_zhct') {
    throw new RuntimeException('Unexpected store database target');
}
$db = get_ai_db();
$database = (string)$db->query('SELECT DATABASE() AS database_name')[0]['database_name'];
if ($database !== 'ai_zhct') {
    throw new RuntimeException('Unexpected connected database');
}
$userId = (int)($argv[2] ?? 0);
$user = $db->name('user')->where('user_id', $userId)->field('user_id,nick_name')->find();
if (!$user || strpos((string)$user['nick_name'], 'VWCG-1381-LOCAL-') !== 0) {
    throw new RuntimeException('Fixture user not found');
}

$action = $argv[1] ?? '';
if ($action === 'system-expire') {
    $now = time();
    $grantId = (int)$db->name('user_subsidy')->insertGetId([
        'user_id' => $userId,
        'money' => '20.00',
        'store_id' => 10001,
        'is_delete' => 0,
        'is_expire' => 1,
        'expiration_start_time' => '2020-01-01 00:00:00',
        'expiration_end_time' => '2020-12-31 23:59:59',
    ]);
    $grantLogId = (int)$db->name('user_subsidy_log')->insertGetId([
        'scene' => 10,
        'describe' => 'VWCG1381测试补贴发放',
        'store_id' => 10001,
        'order_id' => $grantId,
        'create_time' => $now,
        'user_id' => $userId,
        'money' => '20.00',
        'subsidy_balance_after' => '110.00',
        'operator_name' => 'VWCG1381测试员',
        'remark' => 'VWCG-1381-LOCAL',
    ]);
    $expireLog = [
        'scene' => 50,
        'describe' => '失效：自动',
        'store_id' => 10001,
        'order_id' => $grantId,
        'create_time' => $now + 1,
        'user_id' => $userId,
        'money' => '-20.00',
        'remark' => 'VWCG-1381-LOCAL',
    ];
    $expireLog = \app\common\service\UserSubsidyLogService::fillLogFields($db, $expireLog, '自动');
    $expireLogId = (int)$db->name('user_subsidy_log')->insertGetId($expireLog);
    if ($expireLog['subsidy_balance_after'] !== '90.00' || $expireLog['operator_name'] !== '自动') {
        throw new RuntimeException('System expiry fields did not match');
    }
    echo json_encode(compact('database', 'userId', 'grantId', 'grantLogId', 'expireLogId', 'expireLog'), JSON_UNESCAPED_UNICODE) . PHP_EOL;
    exit;
}

if ($action === 'verify') {
    $cashLog = $db->name('user_balance_log')->where('user_id', $userId)->order('log_id', 'desc')->find();
    $subsidyLog = $db->name('user_subsidy_log')->where('user_id', $userId)->order('log_id', 'desc')->find();
    $logic = new \app\p\logic\WalletFlow();
    $cashResult = $logic->getList([
        'keyword' => 'CASH--' . $cashLog['log_id'],
        'account_type' => 'cash',
        'page' => 1,
        'page_size' => 15,
    ]);
    $subsidyResult = $logic->getList([
        'keyword' => 'SUBSIDY--' . $subsidyLog['log_id'],
        'account_type' => 'subsidy',
        'page' => 1,
        'page_size' => 15,
    ]);
    if ((int)$cashResult['total'] !== 1 || (int)$subsidyResult['total'] !== 1
        || $subsidyResult['data'][0]['balance_after'] !== '90.00'
        || $subsidyResult['data'][0]['operator_name'] !== '自动') {
        throw new RuntimeException('WalletFlow query did not return fixture: ' . json_encode(compact('cashResult', 'subsidyResult'), JSON_UNESCAPED_UNICODE));
    }
    echo json_encode([
        'database' => $database,
        'userId' => $userId,
        'cash' => $cashResult['data'][0],
        'subsidy' => $subsidyResult['data'][0],
    ], JSON_UNESCAPED_UNICODE) . PHP_EOL;
    exit;
}

throw new InvalidArgumentException('Unknown action');
