<?php

namespace Tests\Feature;

use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; 
use Tests\TestCase;

class AuthenticationTest extends TestCase
{
    use RefreshDatabase;

    public function test_login_screen_can_be_rendered()
    {
        $response = $this->get('/login');

        $response->assertStatus(200);
    }

   

    public function test_user_redirects_to_dashboard_in_preferred_locale_on_login()
    {
        $preferredLocale = 'en'; // Example user preference
        $user = User::factory()->create([
            'password' => bcrypt('password'),
            'lang_preference' => $preferredLocale,
        ]);


        $this->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class);

        $response = $this->post(route('login'), [
            'name' => $user->email,
            'password' => 'password',
        ]);

        $this->assertAuthenticatedAs($user);

        $expectedRedirectUrl = LaravelLocalization::getURLFromRouteNameTranslated(
            $preferredLocale,
            'routes.dashboard'
        );
        $response->assertRedirect($expectedRedirectUrl);
    }




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

        $this->post('/login', [
            'name' => $user->email,
            'password' => 'wrong-password',
        ]);

        $this->assertGuest();
    }
}
