<?php

namespace Tests\Feature;

use App\Models\GameConfig;
use App\Models\LotteryIssue;
use App\Models\LotteryTicket;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LotteryControllerTest extends TestCase
{
    use RefreshDatabase;

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

        GameConfig::updateOrCreate(
            ['game_key' => 'lottery'],
            [
                'name' => 'Lottery',
                'icon' => 'lottery',
                'description' => 'Lottery Game',
                'enabled' => true,
                'params' => [
                    'ticket_price' => 100,
                ],
            ]
        );
    }

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

        $issue = LotteryIssue::create([
            'issue_no' => '2026101',
            'status' => 'open',
            'red1' => 0,
            'red2' => 0,
            'red3' => 0,
            'blue' => 0,
            'pool_amount' => 5000,
            'carry_amount' => 0,
            'is_super_issue' => false,
            'no_winner_streak' => 0,
            'total_tickets' => 0,
            'payout_amount' => 0,
            'sell_closes_at' => now()->addMinutes(1),
            'draw_at' => now()->addMinutes(2),
        ]);

        $response = $this->actingAs($user)->getJson(route('lottery.current'));

        $response->assertStatus(200);
        $response->assertJsonStructure(['issue' => ['id', 'issue_no', 'status', 'pool_amount']]);
        $this->assertEquals($issue->id, $response->json('issue.id'));
    }

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

        $response = $this->actingAs($user)->getJson(route('lottery.quick-pick', ['count' => 2]));

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

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

        LotteryIssue::create([
            'issue_no' => '2026101',
            'status' => 'open',
            'red1' => 0,
            'red2' => 0,
            'red3' => 0,
            'blue' => 0,
            'pool_amount' => 5000,
            'carry_amount' => 0,
            'is_super_issue' => false,
            'no_winner_streak' => 0,
            'total_tickets' => 0,
            'payout_amount' => 0,
            'sell_closes_at' => now()->addMinutes(1),
            'draw_at' => now()->addMinutes(2),
        ]);

        $response = $this->actingAs($user)->postJson(route('lottery.buy'), [
            'numbers' => [
                [
                    'reds' => [1, 2, 3],
                    'blue' => 4,
                ],
            ],
            'quick_pick' => false,
        ]);

        $response->assertStatus(422); // Exception thrown with 422
    }

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

        LotteryIssue::create([
            'issue_no' => '2026101',
            'status' => 'open',
            'red1' => 0,
            'red2' => 0,
            'red3' => 0,
            'blue' => 0,
            'pool_amount' => 5000,
            'carry_amount' => 0,
            'is_super_issue' => false,
            'no_winner_streak' => 0,
            'total_tickets' => 0,
            'payout_amount' => 0,
            'sell_closes_at' => now()->addMinutes(1),
            'draw_at' => now()->addMinutes(2),
        ]);

        $response = $this->actingAs($user)->postJson(route('lottery.buy'), [
            'numbers' => [
                [
                    'reds' => [1, 2, 3],
                    'blue' => 4,
                ],
            ],
            'quick_pick' => false,
        ]);

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

        $this->assertEquals(100, $user->fresh()->jjb); // cost is 100 per ticket
    }

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

        LotteryIssue::create([
            'issue_no' => '2026100',
            'status' => 'settled',
            'red1' => 1,
            'red2' => 2,
            'red3' => 3,
            'blue' => 4,
            'pool_amount' => 5000,
            'carry_amount' => 0,
            'is_super_issue' => false,
            'no_winner_streak' => 0,
            'total_tickets' => 10,
            'payout_amount' => 1000,
            'draw_at' => now(),
        ]);

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

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

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

        $issue = LotteryIssue::create([
            'issue_no' => '2026100',
            'status' => 'settled',
            'red1' => 1,
            'red2' => 2,
            'red3' => 3,
            'blue' => 4,
            'pool_amount' => 5000,
            'carry_amount' => 0,
            'is_super_issue' => false,
            'no_winner_streak' => 0,
            'total_tickets' => 10,
            'payout_amount' => 1000,
            'draw_at' => now(),
        ]);

        LotteryTicket::create([
            'issue_id' => $issue->id,
            'user_id' => $user->id,
            'red1' => 1,
            'red2' => 2,
            'red3' => 3,
            'blue' => 4,
            'amount' => 100,
            'is_quick_pick' => 0,
        ]);

        $response = $this->actingAs($user)->getJson(route('lottery.my'));

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