<?php

namespace Tests\Feature\Auth;

use App\Models\Province;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class LoginTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_user_can_login()
    {
        $user = User::factory()
            ->create([
                'password' => bcrypt('password'),
                'province_id' => Province::factory()
            ]);

        $response = $this->postJson('/server/auth/login', [
            'username' => $user->username,
            'password' => 'password',
            'telephone_id' => $this->faker->numerify('TID####')
        ]);

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

        $this->assertAuthenticatedAs($user);
    }

    public function test_username_is_required()
    {
        $response = $this->postJson('/server/auth/login', [
            'password' => 'password',
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'username' => __('validation.required', ['attribute' => 'نام کاربری']),
            ]);
    }


    public function test_password_is_required()
    {
        $response = $this->postJson('/server/auth/login', [
            'username' => $this->faker->lexify('?????'),
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'password' => __('validation.required', ['attribute' => 'رمز عبور'])
            ]);
    }

    public function test_telephone_id_is_required()
    {
        $response = $this->postJson('/server/auth/login', [
            'username' => $this->faker->lexify('?????'),
            'password' => 'password',
        ]);
        $response->assertUnprocessable()
            ->assertInvalid([
                'telephone_id' => __('validation.required', ['attribute' => 'کد تلفن'])
            ]);
    }
}
