<?php

namespace Tests\Unit;

use App\Support\FormTextRules;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

class FormTextRulesTest extends TestCase
{
    public function test_not_numeric_only_rejects_digit_only_values(): void
    {
        $validator = Validator::make(
            ['name' => '12345'],
            ['name' => FormTextRules::notNumericOnly(true, 191)],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('name'));
    }

    public function test_not_numeric_only_accepts_mixed_text(): void
    {
        $validator = Validator::make(
            ['name' => 'Laptop 15'],
            ['name' => FormTextRules::notNumericOnly(true, 191)],
            FormTextRules::messages()
        );

        $this->assertFalse($validator->fails(), $validator->errors()->toJson());
    }

    public function test_leave_reason_rejects_numeric_only_and_half_leave_required_when_half_day(): void
    {
        $reason = Validator::make(
            ['reason' => '999'],
            ['reason' => FormTextRules::notNumericOnly(true, 1000)],
            FormTextRules::messages()
        );
        $this->assertTrue($reason->fails());

        $half = Validator::make(
            ['is_half_day' => '1', 'half_leave' => ''],
            [
                'is_half_day' => ['nullable', 'boolean'],
                'half_leave' => ['required_if:is_half_day,1', 'nullable', 'in:first_half,second_half'],
            ],
            FormTextRules::messages()
        );
        $this->assertTrue($half->fails());
        $this->assertTrue($half->errors()->has('half_leave'));
    }

    public function test_shift_name_rejects_numeric_only(): void
    {
        $validator = Validator::make(
            ['name' => '123'],
            ['name' => FormTextRules::notNumericOnly(true, 191)],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('name'));
    }

    public function test_identical_clock_times_can_be_detected_after_normalize(): void
    {
        $in = '09:00:00';
        $out = '09:00:00';
        $this->assertSame($in, $out);
    }

    public function test_appreciation_title_and_future_date_rejected(): void
    {
        $title = Validator::make(
            ['title' => '42'],
            ['title' => FormTextRules::notNumericOnly(true, 191)],
            FormTextRules::messages()
        );
        $this->assertTrue($title->fails());

        $date = Validator::make(
            ['appreciation_date' => now()->addDay()->toDateString()],
            ['appreciation_date' => 'required|date|before_or_equal:today'],
            FormTextRules::messages()
        );
        $this->assertTrue($date->fails());
        $this->assertTrue($date->errors()->has('appreciation_date'));
    }

    public function test_resignation_last_working_date_must_be_on_or_after_resignation_date(): void
    {
        $validator = Validator::make(
            [
                'resignation_date' => '2026-06-01',
                'last_working_date' => '2026-05-01',
            ],
            [
                'resignation_date' => 'required|date',
                'last_working_date' => 'nullable|date|after_or_equal:resignation_date',
            ],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('last_working_date'));
    }

    public function test_reason_and_notes_messages_exist(): void
    {
        $messages = FormTextRules::messages();
        $this->assertArrayHasKey('reason.not_regex', $messages);
        $this->assertArrayHasKey('notes.not_regex', $messages);
        $this->assertArrayHasKey('reason_type.not_regex', $messages);
        $this->assertArrayHasKey('half_leave.required_if', $messages);
    }

    public function test_issued_on_rejects_future_dates(): void
    {
        $validator = Validator::make(
            ['issued_on' => now()->addDay()->toDateString()],
            ['issued_on' => 'nullable|date|before_or_equal:today'],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('issued_on'));
    }
}
