<?php

namespace Tests\Unit;

use App\Support\EmployeeValidation;
use App\Support\FormTextRules;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

class CareersAndReviewsValidationTest extends TestCase
{
    public function test_careers_name_rejects_numeric_only(): void
    {
        $validator = Validator::make(
            ['name' => '12345'],
            ['name' => ['required', 'string', 'max:191', 'regex:'.EmployeeValidation::ALPHA_SPACES]],
            ['name.regex' => EmployeeValidation::messages()['name.regex']]
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('name'));
    }

    public function test_careers_email_normalize_lowercases(): void
    {
        $normalized = EmployeeValidation::normalize(['email' => 'Applicant@Example.COM']);
        $this->assertSame('applicant@example.com', $normalized['email']);
    }

    public function test_careers_phone_rejects_short_and_negative(): void
    {
        $messages = ['phone.digits' => EmployeeValidation::messages()['phone.digits']];

        $short = Validator::make(
            ['phone' => '123'],
            ['phone' => ['nullable', 'digits:10']],
            $messages
        );
        $this->assertTrue($short->fails());

        $negative = Validator::make(
            ['phone' => '-987654321'],
            ['phone' => ['nullable', 'digits:10']],
            $messages
        );
        $this->assertTrue($negative->fails());
    }

    public function test_careers_resume_is_required(): void
    {
        $validator = Validator::make(
            [],
            ['resume' => ['required', 'file', 'mimes:pdf,doc,docx', 'max:5120']],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('resume'));
        $this->assertSame('Please upload a resume.', $validator->errors()->first('resume'));
    }

    public function test_careers_cover_note_rejects_numeric_only(): void
    {
        $validator = Validator::make(
            ['notes' => '98765'],
            ['notes' => FormTextRules::notNumericOnly(false, 2000)],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('notes'));
    }

    public function test_review_period_and_rating_are_required(): void
    {
        $validator = Validator::make(
            ['period' => '', 'rating' => ''],
            [
                'period' => FormTextRules::notNumericOnly(true, 50),
                'rating' => ['required', 'numeric', 'min:0', 'max:5'],
            ],
            array_merge(FormTextRules::messages(), [
                'period.required' => 'Please enter a review period.',
                'rating.required' => 'Please enter a rating.',
            ])
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('period'));
        $this->assertTrue($validator->errors()->has('rating'));
    }

    public function test_review_period_rejects_numeric_only(): void
    {
        $validator = Validator::make(
            ['period' => '123'],
            ['period' => FormTextRules::notNumericOnly(true, 50)],
            array_merge(FormTextRules::messages(), [
                'period.not_regex' => 'The period cannot be numbers only.',
            ])
        );

        $this->assertTrue($validator->fails());
        $this->assertSame('The period cannot be numbers only.', $validator->errors()->first('period'));
    }

    public function test_review_notes_reject_numeric_only(): void
    {
        $validator = Validator::make(
            ['notes' => '123456'],
            ['notes' => FormTextRules::notNumericOnly(false, 5000)],
            FormTextRules::messages()
        );

        $this->assertTrue($validator->fails());
        $this->assertTrue($validator->errors()->has('notes'));
    }
}
