<?php

namespace Tests\Unit\Sale;

use App\Domains\Sale\Requests\AdminAppSaleModificationPdfRequest;
use App\Domains\Sale\Requests\AdminAppSalePdfRequest;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

class AdminAppSalePdfRequestTest extends TestCase
{
    public function test_pdf_requests_accept_php_timezone_identifiers_including_browser_aliases(): void
    {
        $requests = [
            new AdminAppSalePdfRequest,
            new AdminAppSaleModificationPdfRequest,
        ];

        foreach ($requests as $request) {
            $this->assertTrue(Validator::make([
                'timezone' => 'America/Argentina/Buenos_Aires',
            ], $request->rules())->passes());

            $this->assertTrue(Validator::make([
                'timezone' => 'America/Buenos_Aires',
            ], $request->rules())->passes());

            $this->assertFalse(Validator::make([
                'timezone' => 'Invalid/Timezone',
            ], $request->rules())->passes());

            $this->assertFalse(Validator::make([], $request->rules())->passes());
        }
    }

    public function test_invalid_timezone_message_uses_the_application_locale(): void
    {
        $request = new AdminAppSalePdfRequest;

        App::setLocale('es');
        $spanishValidator = Validator::make(['timezone' => 'Invalid/Timezone'], $request->rules());
        $this->assertSame(
            'La zona horaria enviada por el navegador no es válida.',
            $spanishValidator->errors()->first('timezone'),
        );

        App::setLocale('en');
        $englishValidator = Validator::make(['timezone' => 'Invalid/Timezone'], $request->rules());
        $this->assertSame(
            'The timezone sent by the browser is invalid.',
            $englishValidator->errors()->first('timezone'),
        );
    }
}
