<?php

use App\Models\Hunts;
use App\Models\HuntSteps;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\Fluent\AssertableJson;

uses(RefreshDatabase::class);

it('shows a hunt with participants, steps, and total step xp', function () {
    $user = User::factory()->create();
    $participant = User::factory()->create([
        'avatar_uri' => 'avatars/participant.png',
        'xp' => 450,
    ]);
    $hunt = Hunts::withoutSyncingToSearch(fn () => Hunts::factory()->create([
        'title' => 'Treasure hunt',
    ]));

    $hunt->participants()->attach($participant->id, [
        'current_step_number' => 1,
        'status' => 'in_progress',
    ]);

    HuntSteps::factory()->create([
        'hunt_id' => $hunt->id,
        'step_number' => 1,
        'title' => 'First step',
        'xp' => 125,
    ]);

    HuntSteps::factory()->create([
        'hunt_id' => $hunt->id,
        'step_number' => 2,
        'title' => 'Second step',
        'xp' => 75,
    ]);

    $response = $this
        ->actingAs($user, 'sanctum')
        ->getJson("/api/hunts/{$hunt->id}");

    $response
        ->assertOk()
        ->assertJson(fn (AssertableJson $json) => $json
            ->where('id', $hunt->id)
            ->where('title', 'Treasure hunt')
            ->where('xp', 200)
            ->has('participants', 1)
            ->where('participants.0.id', $participant->id)
            ->where('participants.0.username', $participant->username)
            ->has('steps', 2)
            ->etc()
        );
});
