<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\Category;
use App\Models\Screening;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;

class ScreeningScoringTest extends TestCase
{
    public function test_all_yes_answers_pass_screening(): void
    {
        $user = $this->createAuthenticatedUser();
        $screening = Screening::factory()->create(['user_id' => $user->id]);

        $answers = array_fill(1, 10, 'yes');

        $this->put("/screening/{$screening->id}", ['answers' => $answers])
            ->assertRedirect("/screening/{$screening->id}/result");

        $screening->refresh();

        $this->assertEquals(10, $screening->score);
        $this->assertTrue($screening->passed);
    }

    public function test_all_no_answers_fail_screening(): void
    {
        $user = $this->createAuthenticatedUser();
        $screening = Screening::factory()->create(['user_id' => $user->id]);

        $answers = array_fill(1, 10, 'no');

        $this->put("/screening/{$screening->id}", ['answers' => $answers])
            ->assertRedirect("/screening/{$screening->id}/result");

        $screening->refresh();

        $this->assertEquals(0, $screening->score);
        $this->assertFalse($screening->passed);
    }

    public function test_exactly_five_yes_answers_pass_screening_boundary(): void
    {
        $user = $this->createAuthenticatedUser();
        $screening = Screening::factory()->create(['user_id' => $user->id]);

        $answers = [
            1 => 'yes',
            2 => 'yes',
            3 => 'yes',
            4 => 'yes',
            5 => 'yes',
            6 => 'no',
            7 => 'no',
            8 => 'no',
            9 => 'no',
            10 => 'no',
        ];

        $this->put("/screening/{$screening->id}", ['answers' => $answers])
            ->assertRedirect("/screening/{$screening->id}/result");

        $screening->refresh();

        $this->assertEquals(5, $screening->score);
        $this->assertTrue($screening->passed);
    }

    public function test_four_yes_answers_fail_screening_boundary(): void
    {
        $user = $this->createAuthenticatedUser();
        $screening = Screening::factory()->create(['user_id' => $user->id]);

        $answers = [
            1 => 'yes',
            2 => 'yes',
            3 => 'yes',
            4 => 'yes',
            5 => 'no',
            6 => 'no',
            7 => 'no',
            8 => 'no',
            9 => 'no',
            10 => 'no',
        ];

        $this->put("/screening/{$screening->id}", ['answers' => $answers])
            ->assertRedirect("/screening/{$screening->id}/result");

        $screening->refresh();

        $this->assertEquals(4, $screening->score);
        $this->assertFalse($screening->passed);
    }

    public function test_result_page_shows_categories_when_passed(): void
    {
        $user = $this->createAuthenticatedUser();
        $screening = Screening::factory()->create([
            'user_id' => $user->id,
            'score' => 10,
            'passed' => true,
        ]);

        Category::factory()->count(6)->create();

        $this->get("/screening/{$screening->id}/result")
            ->assertInertia(fn (Assert $page) => $page
                ->component('Screening/Result')
                ->has('categories', 6)
                ->where('passed', true)
                ->where('score', 10)
            );
    }

    public function test_result_page_shows_no_categories_when_failed(): void
    {
        $user = $this->createAuthenticatedUser();
        $screening = Screening::factory()->create([
            'user_id' => $user->id,
            'score' => 0,
            'passed' => false,
        ]);

        Category::factory()->count(6)->create();

        $this->get("/screening/{$screening->id}/result")
            ->assertInertia(fn (Assert $page) => $page
                ->component('Screening/Result')
                ->has('categories', 0)
                ->where('passed', false)
                ->where('score', 0)
            );
    }
}
