<?php

namespace Tests\Unit;

use App\Support\EmployeeValidation;
use App\Support\FormTextRules;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

class OrgMastersValidationTest extends TestCase
{
    public function test_department_and_designation_name_rejects_numeric_only(): void
    {
        foreach (['12345', '999'] as $value) {
            $validator = Validator::make(
                ['name' => $value],
                ['name' => FormTextRules::notNumericOnly(true, 191)],
                FormTextRules::messages()
            );
            $this->assertTrue($validator->fails(), "Expected failure for {$value}");
        }

        $ok = Validator::make(
            ['name' => 'Engineering'],
            ['name' => FormTextRules::notNumericOnly(true, 191)],
            FormTextRules::messages()
        );
        $this->assertFalse($ok->fails());
    }

    public function test_location_address_rejects_numeric_only(): void
    {
        $validator = Validator::make(
            ['address' => '42'],
            ['address' => FormTextRules::notNumericOnly(false, 2000)],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('address'));
    }

    public function test_holiday_and_leave_type_name_rejects_numeric_only(): void
    {
        $holiday = Validator::make(
            ['name' => '101'],
            ['name' => FormTextRules::notNumericOnly(true, 191)],
            FormTextRules::messages()
        );
        $this->assertTrue($holiday->fails());

        $leave = Validator::make(
            ['name' => '7', 'description' => '888'],
            [
                'name' => FormTextRules::notNumericOnly(true, 191),
                'description' => FormTextRules::notNumericOnly(false, 5000),
            ],
            FormTextRules::messages()
        );
        $this->assertTrue($leave->fails());
        $this->assertTrue($leave->errors()->has('name'));
        $this->assertTrue($leave->errors()->has('description'));
    }

    public function test_settings_phone_must_be_ten_digits(): void
    {
        $short = Validator::make(
            ['phone' => '12345'],
            ['phone' => ['nullable', 'digits:10']],
            ['phone.digits' => EmployeeValidation::messages()['phone.digits']]
        );
        $this->assertTrue($short->fails());

        $ok = Validator::make(
            ['phone' => '9876543210'],
            ['phone' => ['nullable', 'digits:10']],
            ['phone.digits' => EmployeeValidation::messages()['phone.digits']]
        );
        $this->assertFalse($ok->fails());
    }

    public function test_payee_style_contact_rules(): void
    {
        $messages = array_merge(FormTextRules::messages(), [
            'phone.digits' => EmployeeValidation::messages()['phone.digits'],
        ]);

        $validator = Validator::make(
            [
                'name' => '55',
                'phone' => '-123',
                'email' => 'bad',
            ],
            [
                'name' => FormTextRules::notNumericOnly(true, 191),
                'phone' => ['nullable', 'digits:10'],
                'email' => ['nullable', 'email', 'max:191'],
            ],
            $messages
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('name'));
        $this->assertTrue($validator->errors()->has('phone'));
        $this->assertTrue($validator->errors()->has('email'));
    }

    public function test_terminal_alias_rejects_numeric_only(): void
    {
        $validator = Validator::make(
            ['terminal_alias' => '123'],
            ['terminal_alias' => FormTextRules::notNumericOnly(false, 191)],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertSame(
            'The terminal alias cannot be numbers only.',
            $validator->errors()->first('terminal_alias')
        );
    }

    public function test_weekoff_assign_required_messages_exist(): void
    {
        $messages = FormTextRules::messages();
        $this->assertArrayHasKey('weekoff_rule_id.required', $messages);
        $this->assertArrayHasKey('user_ids.required', $messages);
        $this->assertArrayHasKey('address.not_regex', $messages);
    }

    public function test_company_email_normalize_lowercases(): void
    {
        $normalized = EmployeeValidation::normalize(['email' => 'HR@Company.COM', 'phone' => ' 9876543210 ']);
        $this->assertSame('hr@company.com', $normalized['email']);
        $this->assertSame('9876543210', $normalized['phone']);
    }
}
