<?php

namespace Tests\Feature;

use App\Models\GameConfig;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Redis;
use Tests\TestCase;

class FishingControllerTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        Redis::flushall();

        GameConfig::updateOrCreate(
            ['game_key' => 'fishing'],
            [
                'name' => 'Fishing',
                'icon' => 'fish',
                'description' => 'Fishing Game',
                'enabled' => true,
                'params' => [
                    'fishing_cost' => 5,
                    'fishing_cooldown' => 300,
                ],
            ]
        );
    }

    public function test_can_cast_rod()
    {
        /** @var \App\Models\User $user */
        $user = User::factory()->create(['jjb' => 10]);

        $response = $this->actingAs($user)->postJson(route('fishing.cast', ['id' => 1]));

        $response->assertStatus(200);
        $response->assertJson([
            'status' => 'success',
            'cost' => 5,
        ]);

        $this->assertEquals(5, $user->fresh()->jjb);
    }

    public function test_cannot_cast_when_on_cooldown()
    {
        /** @var \App\Models\User $user */
        $user = User::factory()->create(['jjb' => 10]);
        Redis::setex("fishing:cd:{$user->id}", 100, time());

        $response = $this->actingAs($user)->postJson(route('fishing.cast', ['id' => 1]));

        $response->assertStatus(429);
        $response->assertJson([
            'status' => 'error',
        ]);
    }

    public function test_cannot_cast_without_enough_gold()
    {
        /** @var \App\Models\User $user */
        $user = User::factory()->create(['jjb' => 2]);

        $response = $this->actingAs($user)->postJson(route('fishing.cast', ['id' => 1]));

        $response->assertStatus(422);
        $response->assertJson([
            'status' => 'error',
        ]);
    }

    public function test_can_reel_after_waiting()
    {
        /** @var \App\Models\User $user */
        $user = User::factory()->create(['jjb' => 10]);

        $token = 'test-token';
        $waitTime = 0; // Set to 0 so we can test immediately

        Redis::set("fishing:token:{$user->id}", json_encode([
            'token' => $token,
            'cast_at' => time() - 1, // Simulate past
            'wait_time' => 0,
        ]));

        $response = $this->actingAs($user)->postJson(route('fishing.reel', ['id' => 1]), [
            'token' => $token,
        ]);

        $response->assertStatus(200);
        $response->assertJson([
            'status' => 'success',
        ]);

        // Cooldown should be set
        $this->assertTrue((bool) Redis::exists("fishing:cd:{$user->id}"));
    }

    public function test_cannot_reel_with_invalid_token()
    {
        /** @var \App\Models\User $user */
        $user = User::factory()->create(['jjb' => 10]);

        Redis::set("fishing:token:{$user->id}", json_encode([
            'token' => 'valid-token',
            'cast_at' => time(),
            'wait_time' => 0,
        ]));

        $response = $this->actingAs($user)->postJson(route('fishing.reel', ['id' => 1]), [
            'token' => 'invalid-token',
        ]);

        $response->assertStatus(422);
        $response->assertJson([
            'status' => 'error',
        ]);
    }
}
