<?php

namespace Tests\Unit\Http\Requests\AttendanceCommentTemplate;

use App\Http\Requests\AttendanceCommentTemplate\StoreAttendanceCommentTemplateRequest;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

class StoreAttendanceCommentTemplateRequestTest extends TestCase
{
    private function makeRequest(): StoreAttendanceCommentTemplateRequest
    {
        return new StoreAttendanceCommentTemplateRequest();
    }

    public function test_rules_pass_with_valid_data(): void
    {
        $request = $this->makeRequest();

        $data = [
            'min_score' => 0,
            'max_score' => 100,
            'template_text' => 'Valid template text',
            'is_active' => true,
        ];

        $validator = Validator::make($data, $request->rules());

        $this->assertFalse($validator->fails(), json_encode($validator->errors()->toArray()));
    }

    public function test_rules_fail_when_required_fields_missing(): void
    {
        $request = $this->makeRequest();

        $validator = Validator::make([], $request->rules());

        $this->assertTrue($validator->fails());
        $this->assertArrayHasKey('min_score', $validator->errors()->toArray());
        $this->assertArrayHasKey('max_score', $validator->errors()->toArray());
        $this->assertArrayHasKey('template_text', $validator->errors()->toArray());
    }

    public function test_rules_fail_when_max_score_less_than_min_score(): void
    {
        $request = $this->makeRequest();

        $data = [
            'min_score' => 90,
            'max_score' => 50,
            'template_text' => 'Invalid range',
            'is_active' => true,
        ];

        $validator = Validator::make($data, $request->rules());

        $this->assertTrue($validator->fails());
        $this->assertArrayHasKey('max_score', $validator->errors()->toArray());
    }

    public function test_rules_fail_when_score_out_of_range(): void
    {
        $request = $this->makeRequest();

        $data = [
            'min_score' => -1,
            'max_score' => 101,
            'template_text' => 'Out of range',
            'is_active' => true,
        ];

        $validator = Validator::make($data, $request->rules());

        $this->assertTrue($validator->fails());
        $this->assertArrayHasKey('min_score', $validator->errors()->toArray());
        $this->assertArrayHasKey('max_score', $validator->errors()->toArray());
    }
}