<?php

use App\Models\AdminUser;
use App\Services\Agent\AgentNodeService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});

test('creating child agent with relative_share_rate calculates total_share_rate correctly', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');

    $super = AdminUser::query()->create([
        'username' => 'rel_super',
        'name' => 'Rel',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);

    // 创建 A，总占成 20%
    $agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
        'parent_id' => $rootId,
        'code' => 'REL_A',
        'name' => 'Agent A',
        'username' => 'rel_agent_a',
        'total_share_rate' => 20,
        'credit_limit' => 100000,
    ]));

    // 用 relative_share_rate 创建 B，输入 50（即 A 的 50% = 10%）
    $agentB = app(AgentNodeService::class)->createChild($super, array_merge(
        agentChildPayload([
            'parent_id' => $agentA->id,
            'code' => 'REL_B',
            'name' => 'Agent B',
            'username' => 'rel_agent_b',
            'credit_limit' => 50000,
        ]),
        ['relative_share_rate' => 50]
    ));

    // 验证 B 的实际总占成是 10%
    $profileB = DB::table('agent_profiles')->where('agent_node_id', $agentB->id)->first();
    expect($profileB)->not->toBeNull();
    expect((float) $profileB->total_share_rate)->toBe(10.0);
});

test('relative_share_rate 100 gives same total as parent', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');

    $super = AdminUser::query()->create([
        'username' => 'rel_super2',
        'name' => 'Rel2',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);

    $agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
        'parent_id' => $rootId,
        'code' => 'REL2_A',
        'name' => 'Agent A',
        'username' => 'rel2_agent_a',
        'total_share_rate' => 30,
        'credit_limit' => 100000,
    ]));

    $agentB = app(AgentNodeService::class)->createChild($super, array_merge(
        agentChildPayload([
            'parent_id' => $agentA->id,
            'code' => 'REL2_B',
            'name' => 'Agent B',
            'username' => 'rel2_agent_b',
            'credit_limit' => 50000,
        ]),
        ['relative_share_rate' => 100]
    ));

    $profileB = DB::table('agent_profiles')->where('agent_node_id', $agentB->id)->first();
    expect((float) $profileB->total_share_rate)->toBe(30.0);
});

test('relative_share_rate 0 creates agent with zero share', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');

    $super = AdminUser::query()->create([
        'username' => 'rel_super3',
        'name' => 'Rel3',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);

    $agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
        'parent_id' => $rootId,
        'code' => 'REL3_A',
        'name' => 'Agent A',
        'username' => 'rel3_agent_a',
        'total_share_rate' => 40,
        'credit_limit' => 100000,
    ]));

    $agentB = app(AgentNodeService::class)->createChild($super, array_merge(
        agentChildPayload([
            'parent_id' => $agentA->id,
            'code' => 'REL3_B',
            'name' => 'Agent B',
            'username' => 'rel3_agent_b',
            'credit_limit' => 50000,
        ]),
        ['relative_share_rate' => 0]
    ));

    $profileB = DB::table('agent_profiles')->where('agent_node_id', $agentB->id)->first();
    expect((float) $profileB->total_share_rate)->toBe(0.0);
});
