<?php

namespace Tests\Unit\Http\Requests\AttendanceCommentTemplate;

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

class UpdateAttendanceCommentTemplateRequestTest extends TestCase
{
    private function makeRequest(): UpdateAttendanceCommentTemplateRequest
    {
        return new UpdateAttendanceCommentTemplateRequest();
    }

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

        $data = [
            'template_text' => 'Updated only text',
        ];

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

        // apply after hook manually
        $request->merge($data);
        $request->withValidator($validator);

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

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

        $data = [
            'min_score' => 20,
            'max_score' => 40,
        ];

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

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

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

        $data = [
            'min_score' => 60,
            'max_score' => 10,
        ];

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

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

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

        $data = [
            'is_active' => 'not-bool',
        ];

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

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