<?php

namespace Tests\Feature\Bo;

use App\Models\KycDocument;
use App\Models\Payout;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class BoManagementPagesTest extends TestCase
{
    use RefreshDatabase;

    public function test_guest_is_redirected_for_bo_management_pages(): void
    {
        $this->get(route('bo.users.index'))->assertRedirect(route('login'));
        $this->get(route('bo.missions.index'))->assertRedirect(route('login'));
        $this->get(route('bo.kyc.index'))->assertRedirect(route('login'));
        $this->get(route('bo.payouts.index'))->assertRedirect(route('login'));
    }

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

        KycDocument::create([
            'user_id' => $liner->id,
            'document_type' => 'passport',
            'document_number' => 'KYC-001',
            'status' => 'pending',
        ]);

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

        $this->actingAs($admin)->get(route('bo.users.index'))->assertOk()->assertSee('Utilisateurs BO');
        $this->actingAs($admin)->get(route('bo.missions.index'))->assertOk()->assertSee('Missions BO');
        $this->actingAs($admin)->get(route('bo.kyc.index'))->assertOk()->assertSee('KYC BO');
        $this->actingAs($admin)->get(route('bo.payouts.index'))->assertOk()->assertSee('Retraits BO');
    }

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

        $this->actingAs($admin)->post(route('bo.users.store'), [
            'name' => 'Test User BO',
            'email' => 'test-user-bo@example.com',
            'role' => 'client',
            'phone' => '+33102030405',
            'bio' => 'profil',
            'password' => 'secret1234',
            'password_confirmation' => 'secret1234',
            'is_kyc_verified' => 1,
        ])->assertRedirect(route('bo.users.index'));

        $createdUser = User::where('email', 'test-user-bo@example.com')->firstOrFail();

        $this->actingAs($admin)->put(route('bo.users.update', $createdUser), [
            'name' => 'Test User BO Updated',
            'email' => 'test-user-bo@example.com',
            'role' => 'liner',
            'phone' => '+33102030406',
            'bio' => 'profil update',
            'password' => '',
            'password_confirmation' => '',
            'is_kyc_verified' => 0,
        ])->assertRedirect(route('bo.users.index'));

        $this->assertDatabaseHas('users', [
            'id' => $createdUser->id,
            'name' => 'Test User BO Updated',
            'role' => 'liner',
        ]);

        $this->actingAs($admin)->post(route('bo.missions.store'), [
            'client_id' => $client->id,
            'liner_id' => $liner->id,
            'title' => 'Mission BO CRUD',
            'description' => 'Description mission',
            'event_name' => 'Concert',
            'location' => 'Paris',
            'expected_start_at' => now()->addHour()->format('Y-m-d H:i:s'),
            'status' => 'published',
            'budget_amount' => 89.99,
            'currency' => 'EUR',
        ])->assertRedirect(route('bo.missions.index'));

        $this->assertDatabaseHas('missions', [
            'title' => 'Mission BO CRUD',
            'client_id' => $client->id,
            'liner_id' => $liner->id,
        ]);
    }

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

        Schema::dropIfExists('user_payment_methods');

        $this->actingAs($admin)
            ->get(route('bo.users.index'))
            ->assertOk()
            ->assertSee('Utilisateurs BO');
    }
}
