<?php

namespace Tests\Feature\V3\Profile;

use App\Facades\File\FileFacade;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class EditTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_first_name_is_required()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'));

        $response->assertUnprocessable()
            ->assertInvalid([
                'first_name' => __('validation.required', ['attribute' => 'نام'])
            ]);
    }

    public function test_first_name_should_be_string()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'), [
            'first_name' => $this->faker->numberBetween(0, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'first_name' => __('validation.string', ['attribute' => 'نام'])
            ]);
    }

    public function test_last_name_is_required()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'));

        $response->assertUnprocessable()
            ->assertInvalid([
                'last_name' => __('validation.required', ['attribute' => 'نام خانوادگی'])
            ]);
    }

    public function test_last_name_should_be_string()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'), [
            'last_name' => $this->faker->numberBetween(0, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'last_name' => __('validation.string', ['attribute' => 'نام خانوادگی'])
            ]);
    }

    public function test_degree_is_required()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'));

        $response->assertUnprocessable()
            ->assertInvalid([
                'degree' => __('validation.required', ['attribute' => 'مدرک تحصیلی'])
            ]);
    }

    public function test_major_is_required()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'));

        $response->assertUnprocessable()
            ->assertInvalid([
                'major' => __('validation.required', ['attribute' => 'شغل'])
            ]);
    }

    public function test_mobile_is_required()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'));

        $response->assertUnprocessable()
            ->assertInvalid([
                'mobile' => __('validation.required', ['attribute' => 'تلفن همراه'])
            ]);
    }

    public function test_mobile_should_match_the_regex()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'), [
            'mobile' => $this->faker->numberBetween(1000000, 9999999)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'mobile' => __('validation.regex', ['attribute' => 'تلفن همراه'])
            ]);
    }

    public function test_mobile_should_be_numeric()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'), [
            'mobile' => $this->faker->name()
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'mobile' => __('validation.numeric', ['attribute' => 'تلفن همراه'])
            ]);
    }

    public function test_mobile_should_be_11_digits()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $response = $this->postJson(route('v3.profile.edit'), [
            'mobile' => $this->faker->numberBetween(10000, 99999)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'mobile' => __('validation.digits', ['attribute' => 'تلفن همراه', 'digits' => 11])
            ]);
    }

    public function test_avatar_should_match_the_mimes()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $image = UploadedFile::fake()->create('image.txt', 40);

        $response = $this->postJson(route('v3.profile.edit'), [
            'avatar' => $image
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'avatar' => __('validation.mimes', ['attribute' => 'آواتار', 'values' => 'png, jpg, jpeg'])
            ]);
    }

    public function test_avatar_should_be_less_than_2048kb()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $image = UploadedFile::fake()->create('image.jpg', 40000);

        $response = $this->postJson(route('v3.profile.edit'), [
            'avatar' => $image
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'avatar' => __('validation.max.file', ['attribute' => 'آواتار', 'max' => 2048])
            ]);
    }

    public function test_user_can_edit_the_profile()
    {
        $user = User::factory()->create();

        $this->actingAs($user);

        $avatar = UploadedFile::fake()->create('image.jpg', 1024);

        Storage::fake();

        $data = [
            'first_name' => $this->faker->firstName,
            'last_name' => $this->faker->lastName,
            'degree' => $this->faker->name,
            'major' => $this->faker->name,
            'mobile' => $this->faker->numerify('09#########'),
            'avatar' => $avatar
        ];

        $response = $this->postJson(route('v3.profile.edit'), $data);

        $response->assertOk()
            ->assertExactJson([
                'message' => __('messages.successful')
            ]);

        $this->assertDatabaseHas('users', [
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'mobile' => $data['mobile'],
            'degree' => $data['degree'],
        ]);
    }
}
