<?php

namespace Tests\Feature\Bo;

use App\Models\Mission;
use App\Models\Payment;
use App\Models\PaymentGatewaySetting;
use App\Models\Payout;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class FinancePageTest extends TestCase
{
    use RefreshDatabase;

    public function test_guest_is_redirected_to_login_alias_for_bo_finance(): void
    {
        $this->get(route('bo.finance'))
            ->assertRedirect(route('login'));
    }

    public function test_admin_can_open_finance_page_and_trigger_actions(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);
        $client = User::factory()->create(['role' => 'client']);
        $liner = User::factory()->create(['role' => 'liner']);

        $mission = Mission::create([
            'client_id' => $client->id,
            'liner_id' => $liner->id,
            'title' => 'Mission BO finance',
            'location' => 'Paris',
            'expected_start_at' => now()->addHour(),
            'status' => 'completed',
            'budget_amount' => 50,
            'currency' => 'EUR',
            'completed_at' => now(),
        ]);

        $payment = Payment::create([
            'mission_id' => $mission->id,
            'client_id' => $client->id,
            'liner_id' => $liner->id,
            'amount' => 50,
            'currency' => 'EUR',
            'status' => 'pending',
            'provider' => 'stripe',
        ]);

        $payout = Payout::create([
            'liner_id' => $liner->id,
            'amount' => 30,
            'currency' => 'EUR',
            'provider' => 'rib',
            'status' => 'pending',
            'requested_at' => now(),
        ]);

        $this->actingAs($admin)
            ->get(route('bo.finance'))
            ->assertOk()
            ->assertSee('Finance BO');

        $this->actingAs($admin)
            ->post(route('bo.finance.payments.retry', $payment))
            ->assertRedirect();

        $this->actingAs($admin)
            ->post(route('bo.finance.payouts.process', $payout))
            ->assertRedirect();

        $this->assertDatabaseHas('admin_audit_logs', [
            'admin_id' => $admin->id,
            'action' => 'bo.finance.payment.retry',
        ]);

        $this->assertDatabaseHas('admin_audit_logs', [
            'admin_id' => $admin->id,
            'action' => 'bo.finance.payout.process',
        ]);
    }

    public function test_admin_can_update_gateway_settings_from_finance_page(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);

        $this->actingAs($admin)
            ->post(route('bo.finance.gateway_settings.update'), [
                'simulate' => '0',
                'stripe_secret_key' => 'sk_live_test_123',
                'stripe_webhook_secret' => 'whsec_test_123',
                'paypal_mode' => 'live',
                'paypal_client_id' => 'paypal_client_123',
                'paypal_client_secret' => 'paypal_secret_123',
                'paypal_webhook_id' => 'paypal_webhook_123',
            ])
            ->assertRedirect(route('bo.finance'));

        $global = PaymentGatewaySetting::query()->where('gateway', 'global')->first();
        $stripe = PaymentGatewaySetting::query()->where('gateway', 'stripe')->first();
        $paypal = PaymentGatewaySetting::query()->where('gateway', 'paypal')->first();

        $this->assertNull($global);
        $this->assertNull($stripe);
        $this->assertNull($paypal);

        $this->assertDatabaseHas('admin_audit_logs', [
            'admin_id' => $admin->id,
            'action' => 'bo.finance.gateway_settings.update',
        ]);
    }
}
