<?php

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

uses(RefreshDatabase::class);

test('settlement bills index api resource is configured after migrations', function (): void {
    expect(
        DB::table('admin_api_resources')
            ->where('route_name', 'api.v1.admin.settlement-bills.index')
            ->where('status', 1)
            ->exists(),
    )->toBeTrue();

    $admin = AdminUser::query()->create([
        'username' => 'bill_super',
        'name' => 'Bill Super',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);

    $token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-bills')
        ->assertOk()
        ->assertJsonPath('data.items', fn ($items) => is_array($items));
});

test('settlement bill show returns enriched party labels', 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');
    $childId = (int) DB::table('agent_nodes')->insertGetId([
        'admin_site_id' => $siteId,
        'parent_id' => $rootId,
        'code' => 'bill_show_child',
        'name' => 'Bill Show Child',
        'depth' => 1,
        'path' => '/'.$rootId.'/',
        'status' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    DB::table('agent_nodes')->where('id', $childId)->update([
        'path' => '/'.$rootId.'/'.$childId.'/',
    ]);

    $periodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => $siteId,
        'period_start' => now()->subWeek(),
        'period_end' => now(),
        'status' => 'closed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $billId = (int) DB::table('settlement_bills')->insertGetId([
        'settlement_period_id' => $periodId,
        'bill_type' => 'agent',
        'owner_type' => 'agent',
        'owner_id' => $childId,
        'counterparty_type' => 'agent',
        'counterparty_id' => $rootId,
        'net_amount' => 6400,
        'unpaid_amount' => 6400,
        'paid_amount' => 0,
        'status' => 'confirmed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $admin = AdminUser::query()->create([
        'username' => 'bill_show_super',
        'name' => 'Bill Show Super',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);
    $token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;

    $rootName = (string) DB::table('agent_nodes')->where('id', $rootId)->value('name');

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-bills/'.$billId)
        ->assertOk()
        ->assertJsonPath('data.bill.owner_party_label', 'Bill Show Child')
        ->assertJsonPath('data.bill.superior_agent_label', $rootName);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-bills?bill_id='.$billId)
        ->assertOk()
        ->assertJsonPath('data.items.0.owner_party_label', 'Bill Show Child')
        ->assertJsonPath('data.items.0.superior_agent_label', $rootName);
});

test('settlement bill show returns downline share breakdown for parent agent bill', 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');
    $parentId = (int) DB::table('agent_nodes')->insertGetId([
        'admin_site_id' => $siteId,
        'parent_id' => $rootId,
        'code' => 'downline_parent',
        'name' => 'Downline Parent',
        'depth' => 1,
        'path' => '/'.$rootId.'/',
        'status' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    DB::table('agent_nodes')->where('id', $parentId)->update([
        'path' => '/'.$rootId.'/'.$parentId.'/',
    ]);
    $childId = (int) DB::table('agent_nodes')->insertGetId([
        'admin_site_id' => $siteId,
        'parent_id' => $parentId,
        'code' => 'downline_child',
        'name' => 'Downline Child',
        'depth' => 2,
        'path' => '/'.$rootId.'/'.$parentId.'/',
        'status' => 0,
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    DB::table('agent_nodes')->where('id', $childId)->update([
        'path' => '/'.$rootId.'/'.$parentId.'/'.$childId.'/',
    ]);

    $periodId = (int) DB::table('settlement_periods')->insertGetId([
        'admin_site_id' => $siteId,
        'period_start' => now()->subWeek(),
        'period_end' => now(),
        'status' => 'closed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    DB::table('settlement_bills')->insert([
        'settlement_period_id' => $periodId,
        'bill_type' => 'agent',
        'owner_type' => 'agent',
        'owner_id' => $childId,
        'counterparty_type' => 'agent',
        'counterparty_id' => $parentId,
        'net_amount' => 3916,
        'unpaid_amount' => 3916,
        'paid_amount' => 0,
        'status' => 'confirmed',
        'meta_json' => json_encode(['share_profit' => 484]),
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $parentBillId = (int) DB::table('settlement_bills')->insertGetId([
        'settlement_period_id' => $periodId,
        'bill_type' => 'agent',
        'owner_type' => 'agent',
        'owner_id' => $parentId,
        'counterparty_type' => 'agent',
        'counterparty_id' => $rootId,
        'gross_win_loss' => 4400,
        'net_amount' => 3520,
        'unpaid_amount' => 3520,
        'paid_amount' => 0,
        'status' => 'confirmed',
        'meta_json' => json_encode(['share_profit' => 396]),
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $admin = AdminUser::query()->create([
        'username' => 'downline_share_super',
        'name' => 'Downline Share Super',
        'email' => null,
        'password' => Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($admin);
    $token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->getJson('/api/v1/admin/settlement-bills/'.$parentBillId)
        ->assertOk()
        ->assertJsonPath('data.downline_shares.total', 484)
        ->assertJsonPath('data.downline_shares.items.0.owner_label', 'Downline Child')
        ->assertJsonPath('data.downline_shares.items.0.share_profit', 484);
});
