<?php

namespace Tests\Feature\Bo;

use App\Models\Mission;
use App\Models\MissionDispute;
use App\Models\MissionProof;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ProofAndDisputeModerationTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_can_approve_proof_from_bo(): 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 preuve',
            'location' => 'Paris',
            'expected_start_at' => now()->addHour(),
            'status' => 'arrived',
            'budget_amount' => 50,
            'currency' => 'EUR',
        ]);

        $proof = MissionProof::create([
            'mission_id' => $mission->id,
            'liner_id' => $liner->id,
            'type' => 'photo',
            'status' => 'pending',
        ]);

        $this->actingAs($admin)
            ->post(route('bo.proofs.approve', $proof))
            ->assertRedirect();

        $this->assertDatabaseHas('mission_proofs', [
            'id' => $proof->id,
            'status' => 'approved',
            'reviewed_by' => $admin->id,
        ]);

        $this->assertDatabaseHas('admin_audit_logs', [
            'admin_id' => $admin->id,
            'action' => 'proof.approve',
        ]);
    }

    public function test_admin_can_resolve_dispute_from_bo(): 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 litige',
            'location' => 'Lyon',
            'expected_start_at' => now()->addHour(),
            'status' => 'completed',
            'budget_amount' => 75,
            'currency' => 'EUR',
            'completed_at' => now(),
        ]);

        $dispute = MissionDispute::create([
            'mission_id' => $mission->id,
            'reporter_id' => $client->id,
            'against_user_id' => $liner->id,
            'category' => 'qualite',
            'description' => 'Preuve non conforme.',
            'status' => 'open',
        ]);

        $this->actingAs($admin)
            ->post(route('bo.disputes.resolve', $dispute), [
                'resolution_notes' => 'Controle effectue, dossier clos.',
            ])
            ->assertRedirect();

        $this->assertDatabaseHas('mission_disputes', [
            'id' => $dispute->id,
            'status' => 'resolved',
            'resolved_by' => $admin->id,
        ]);

        $this->assertDatabaseHas('admin_audit_logs', [
            'admin_id' => $admin->id,
            'action' => 'dispute.resolve',
        ]);
    }
}
