<?php

use App\Support\ApiMessage;
use Illuminate\Http\Request;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('api message resolves runtime reason in zh locale', function (): void {
    $request = Request::create('/api/v1/test', 'GET');
    $request->attributes->set('lottery_locale', 'zh');

    expect(ApiMessage::reason($request, 'draw_not_ready_for_jackpot_burst'))
        ->toBe('期号尚未进入结算中或已结算，无法手动爆池。');
});

test('api message resolves success ok in en locale', function (): void {
    $request = Request::create('/api/v1/test', 'GET');
    $request->attributes->set('lottery_locale', 'en');

    expect(ApiMessage::successMessage($request))->toBe('OK');
});

test('api message resolves admin key', function (): void {
    $request = Request::create('/api/v1/test', 'GET');
    $request->attributes->set('lottery_locale', 'zh');

    expect(ApiMessage::get($request, 'admin.site_access_denied'))
        ->toBe('无权访问该站点。');
});

test('api message resolves missing admin player keys', function (): void {
    $request = Request::create('/api/v1/test', 'GET');
    $request->attributes->set('lottery_locale', 'zh');

    expect(ApiMessage::get($request, 'admin.player_native_username_required'))
        ->toBe('请填写彩票端登录账号。');
});

test('api message reason resolves business validation key', function (): void {
    $request = Request::create('/api/v1/test', 'GET');
    $request->attributes->set('lottery_locale', 'zh');

    expect(ApiMessage::reason($request, 'exceeds_available'))
        ->toBe('超出代理可下发额度：请提高该代理授信，或减少其他下级/玩家已占用的额度。');
});

test('api message http exception resolves admin machine key', function (): void {
    $request = Request::create('/api/v1/test', 'GET');
    $request->attributes->set('lottery_locale', 'zh');

    expect(ApiMessage::httpExceptionMessage($request, 403, 'agent_cannot_operate_bill'))
        ->toBe('当前账号无权操作该账单。');
});

test('api message http exception uses permission denied when message empty', function (): void {
    $request = Request::create('/api/v1/test', 'GET');
    $request->attributes->set('lottery_locale', 'zh');

    expect(ApiMessage::httpExceptionMessage($request, 403, ''))
        ->toBe('当前账号无此操作权限。');
});

test('api message http exception resolves admin dotted key', function (): void {
    $request = Request::create('/api/v1/test', 'GET');
    $request->attributes->set('lottery_locale', 'zh');

    expect(ApiMessage::httpExceptionMessage($request, 403, 'admin.agent_bound_cannot_manage_periods'))
        ->toBe('绑定代理账号不能开/关账期，请联系站点财务操作。');
});
