<?php

namespace Tests\Feature;

use App\Models\User;
use App\Services\ReservedAccountProvisioningService;
use App\Services\SystemSettingService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class PaymentPointReservedAccountProvisioningTest extends TestCase
{
    use RefreshDatabase;

    public function test_paymentpoint_reserved_account_can_be_provisioned_for_user(): void
    {
        $user = User::factory()->create([
            'phone' => '08029012017',
        ]);

        app(SystemSettingService::class)->putMany([
            ['section' => 'payments.paymentpoint', 'key' => 'base_url', 'value' => 'https://api.paymentpoint.co', 'type' => 'string'],
            ['section' => 'payments.paymentpoint', 'key' => 'api_key', 'value' => 'pp-api-key-123', 'type' => 'string', 'is_secret' => true],
            ['section' => 'payments.paymentpoint', 'key' => 'secret_key', 'value' => 'pp-secret-key-456', 'type' => 'string', 'is_secret' => true],
            ['section' => 'payments.paymentpoint', 'key' => 'business_id', 'value' => 'BIZ-001', 'type' => 'string'],
            ['section' => 'payments.paymentpoint', 'key' => 'bank_codes', 'value' => ['20946', '20897'], 'type' => 'json'],
        ]);

        Http::fake([
            'https://api.paymentpoint.co/api/v1/createVirtualAccount' => Http::response([
                'status' => 'success',
                'message' => 'Customer account created successfully.',
                'customer' => [
                    'customer_id' => 'pp-customer-001',
                ],
                'bankAccounts' => [
                    [
                        'bankCode' => '20946',
                        'accountNumber' => '6698059290',
                        'accountName' => 'Test User(Paymentpoint)',
                        'bankName' => 'Palmpay',
                        'Reserved_Account_Id' => 'res-1',
                    ],
                    [
                        'bankCode' => '20897',
                        'accountNumber' => '6679854996',
                        'accountName' => 'Test User(Paymentpoint)',
                        'bankName' => 'Opay',
                        'Reserved_Account_Id' => 'res-2',
                    ],
                ],
            ], 200),
        ]);

        $record = app(ReservedAccountProvisioningService::class)
            ->provisionPaymentPointForUser($user);

        $this->assertSame('paymentpoint', $record->provider);
        $this->assertSame('pp-customer-001', $record->account_reference);

        $accounts = data_get($record->accounts_json, 'accounts', []);
        $this->assertCount(2, $accounts);
        $this->assertSame('6698059290', (string) data_get($accounts, '0.accountNumber'));

        $this->assertDatabaseHas('reserved_accounts', [
            'user_id' => $user->id,
            'provider' => 'paymentpoint',
            'account_reference' => 'pp-customer-001',
            'status' => 'active',
        ]);
    }
}

