<?php

namespace Tests\Feature\Feature;

use App\Models\PayoutTransaction;
use App\Models\User;
use App\Models\Withdrawal;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class WithdrawalAdminTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_withdrawals_dashboard_renders_settlement_cards(): void
    {
        $admin = User::query()->create([
            'member_code' => 'AKRWDR001',
            'referral_code' => 'REFWDR001',
            'name' => 'Withdrawal Admin',
            'email' => 'withdraw-admin@example.com',
            'mobile' => '9100000501',
            'password' => 'password',
            'role' => 'admin',
            'status' => 'active',
        ]);

        $member = User::query()->create([
            'member_code' => 'AKRWDR002',
            'referral_code' => 'REFWDR002',
            'name' => 'Withdrawal User',
            'email' => 'withdraw-user@example.com',
            'mobile' => '9100000502',
            'password' => 'password',
            'role' => 'user',
            'status' => 'active',
        ]);

        $withdrawal = Withdrawal::query()->create([
            'user_id' => $member->id,
            'request_number' => 'WDRTST0001',
            'amount' => 1000,
            'tds_amount' => 50,
            'charge_amount' => 20,
            'net_amount' => 930,
            'locked_amount' => 1000,
            'status' => 'pending',
            'requested_at' => now(),
        ]);

        PayoutTransaction::query()->create([
            'withdrawal_id' => $withdrawal->id,
            'user_id' => $member->id,
            'gateway' => 'cashfree',
            'amount' => 930,
            'status' => 'retry',
            'attempts' => 1,
        ]);

        $response = $this->actingAs($admin)->get('/admin/withdrawals');

        $response->assertOk();
        $response->assertSee('Settlement Overview');
        $response->assertSee('TDS / Charge Calculator');
        $response->assertSee('WDRTST0001');
    }

    public function test_admin_can_retry_failed_payout_queue(): void
    {
        $admin = User::query()->create([
            'member_code' => 'AKRWDR010',
            'referral_code' => 'REFWDR010',
            'name' => 'Withdrawal Admin',
            'email' => 'withdraw-admin-2@example.com',
            'mobile' => '9100000510',
            'password' => 'password',
            'role' => 'admin',
            'status' => 'active',
        ]);

        $member = User::query()->create([
            'member_code' => 'AKRWDR011',
            'referral_code' => 'REFWDR011',
            'name' => 'Withdrawal User',
            'email' => 'withdraw-user-2@example.com',
            'mobile' => '9100000511',
            'password' => 'password',
            'role' => 'user',
            'status' => 'active',
        ]);

        $withdrawal = Withdrawal::query()->create([
            'user_id' => $member->id,
            'request_number' => 'WDRTST0010',
            'amount' => 500,
            'tds_amount' => 25,
            'charge_amount' => 10,
            'net_amount' => 465,
            'locked_amount' => 500,
            'status' => 'processing',
            'requested_at' => now(),
        ]);

        $payout = PayoutTransaction::query()->create([
            'withdrawal_id' => $withdrawal->id,
            'user_id' => $member->id,
            'gateway' => 'cashfree',
            'amount' => 465,
            'status' => 'retry',
            'attempts' => 1,
        ]);

        $response = $this->actingAs($admin)->post('/admin/withdrawals/retry-failed', [
            'limit' => 10,
        ]);

        $response->assertRedirect();
        $response->assertSessionHas('status', 'Retried 1 payout records.');

        $payout->refresh();
        $this->assertSame('retry', $payout->status);
        $this->assertSame(2, (int) $payout->attempts);
    }
}

