<?php

namespace Tests\Feature;

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

class GomokuControllerTest extends TestCase
{
    use RefreshDatabase;

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

        GameConfig::updateOrCreate(
            ['game_key' => 'gomoku'],
            [
                'name' => 'Gomoku',
                'icon' => 'gomoku',
                'description' => 'Gomoku Game',
                'enabled' => true,
                'params' => [
                    'invite_timeout' => 60,
                    'pvp_reward' => 80,
                    'pve_easy_fee' => 0,
                    'pve_normal_fee' => 10,
                    'pve_hard_fee' => 30,
                    'pve_expert_fee' => 80,
                ],
            ]
        );
    }

    private function createRoom(int $ownerId)
    {
        return Room::forceCreate([
            'id' => 100, // Safe room id
            'room_name' => 'test',
            'room_auto' => 'open',
            'room_owner' => 'owner',
            'room_des' => 'test description',
            'announcement' => 'welcome',
            'room_top' => '',
            'room_title' => 'test title',
            'room_keep' => 0,
            'room_time' => now(),
            'room_tt' => 0,
            'room_html' => 0,
            'room_exp' => 0,
            'build_time' => now(),
            'permit_level' => 0,
            'door_open' => 1,
            'ooooo' => 0,
            'visit_num' => 0,
        ]);
    }

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

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

        $room = $this->createRoom($owner->id);

        $response = $this->actingAs($user)->postJson(route('gomoku.create'), [
            'mode' => 'pve',
            'room_id' => $room->id,
            'ai_level' => 1,
        ]);

        $response->assertStatus(200);
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('gomoku_games', [
            'mode' => 'pve',
            'player_black_id' => $user->id,
        ]);
    }

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

        /** @var \App\Models\User $owner */
        $owner = User::factory()->create();

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

        $room = $this->createRoom($owner->id);

        $response = $this->actingAs($user)->postJson(route('gomoku.create'), [
            'mode' => 'pvp',
            'room_id' => $room->id,
        ]);

        $response->assertStatus(200);
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('gomoku_games', [
            'mode' => 'pvp',
            'status' => 'waiting',
            'player_black_id' => $user->id,
        ]);
    }

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

        /** @var \App\Models\User $user1 */
        $user1 = User::factory()->create();

        /** @var \App\Models\User $user2 */
        $user2 = User::factory()->create();

        $room = $this->createRoom($owner->id);

        $game = GomokuGame::create([
            'mode' => 'pvp',
            'room_id' => $room->id,
            'player_black_id' => $user1->id,
            'status' => 'waiting',
            'board' => [],
            'current_turn' => 1,
            'entry_fee' => 0,
            'invite_expires_at' => now()->addMinutes(1),
        ]);

        $response = $this->actingAs($user2)->postJson(route('gomoku.join', $game));

        $response->assertStatus(200);
        $response->assertJson(['ok' => true]);
        $this->assertEquals($user2->id, $game->fresh()->player_white_id);
        $this->assertEquals('playing', $game->fresh()->status);
    }

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

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

        $room = $this->createRoom($owner->id);

        $game = GomokuGame::create([
            'mode' => 'pve',
            'room_id' => $room->id,
            'player_black_id' => $user->id,
            'ai_level' => 1,
            'status' => 'playing',
            'board' => [],
            'current_turn' => 1,
            'entry_fee' => 0,
        ]);

        $response = $this->actingAs($user)->getJson(route('gomoku.active'));

        $response->assertStatus(200);
        $response->assertJson([
            'ok' => true,
            'has_active' => true,
            'game_id' => $game->id,
        ]);
    }
}
