<?php

namespace Tests\Feature;

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

class SlotMachineControllerTest extends TestCase
{
    use RefreshDatabase;

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

        GameConfig::updateOrCreate(
            ['game_key' => 'slot_machine'],
            [
                'name' => 'Slot Machine',
                'icon' => 'slot',
                'description' => 'Slot Machine Game',
                'enabled' => true,
                'params' => [
                    'cost_per_spin' => 100,
                    'daily_limit' => 100,
                    'jackpot_payout' => 100,
                    'triple_payout' => 50,
                    'same_payout' => 10,
                    'pair_payout' => 2,
                    'curse_enabled' => true,
                ],
            ]
        );
    }

    public function test_can_get_info()
    {
        /** @var \App\Models\User $user */
        $user = User::factory()->create();

        $response = $this->actingAs($user)->getJson(route('slot.info'));

        $response->assertStatus(200);
        $response->assertJson([
            'enabled' => true,
            'cost_per_spin' => 100,
            'daily_limit' => 100,
            'used_today' => 0,
        ]);
        $response->assertJsonStructure(['symbols']);
    }

    public function test_can_spin_enough_gold()
    {
        Event::fake();

        /** @var \App\Models\User $user */
        $user = User::factory()->create(['jjb' => 200]);

        $response = $this->actingAs($user)->postJson(route('slot.spin'));

        $response->assertStatus(200);
        $response->assertJson(['ok' => true]);

        $this->assertDatabaseHas('slot_machine_logs', [
            'user_id' => $user->id,
            'cost' => 100,
        ]);
    }

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

        $response = $this->actingAs($user)->postJson(route('slot.spin'));

        $response->assertStatus(200);
        $response->assertJson(['ok' => false]);
    }

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

        // Mock daily limit 1
        GameConfig::updateOrCreate(
            ['game_key' => 'slot_machine'],
            [
                'name' => 'Slot Machine',
                'icon' => 'slot',
                'description' => 'Slot Mac',
                'enabled' => true,
                'params' => [
                    'cost_per_spin' => 100,
                    'daily_limit' => 1,
                ],
            ]
        );

        SlotMachineLog::create([
            'user_id' => $user->id,
            'reel1' => 'cherry',
            'reel2' => 'lemon',
            'reel3' => 'orange',
            'result_type' => 'miss',
            'cost' => 100,
            'payout' => 0,
        ]);

        $response = $this->actingAs($user)->postJson(route('slot.spin'));

        $response->assertStatus(200);
        $response->assertJson(['ok' => false]);
    }

    public function test_can_get_history()
    {
        /** @var \App\Models\User $user */
        $user = User::factory()->create();

        SlotMachineLog::create([
            'user_id' => $user->id,
            'reel1' => 'cherry',
            'reel2' => 'cherry',
            'reel3' => 'cherry',
            'result_type' => 'triple',
            'cost' => 100,
            'payout' => 1000,
        ]);

        $response = $this->actingAs($user)->getJson(route('slot.history'));

        $response->assertStatus(200);
        $response->assertJsonStructure(['history']);
        $this->assertCount(1, $response->json('history'));
    }
}
