<?php

namespace Tests\Feature;

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

class LoginTest extends TestCase
{
    use RefreshDatabase;

    public function test_login_empty_fields_returns_proper_error(): void
    {
        $response = $this->post('/login', [
            'login' => '',
            'password' => '',
        ]);

        $response->assertSessionHasErrors([
            'login' => 'Harap isi username dan password.',
        ]);
    }

    public function test_login_correct_username_wrong_password_returns_proper_error(): void
    {
        $user = User::factory()->create([
            'email' => 'admin@sipena.com',
            'password' => bcrypt('password123'),
        ]);

        $response = $this->post('/login', [
            'login' => 'admin@sipena.com',
            'password' => 'wrongpassword',
        ]);

        $response->assertSessionHasErrors([
            'password' => 'Login Gagal. Password salah.',
        ]);
    }

    public function test_login_wrong_username_returns_proper_error(): void
    {
        $response = $this->post('/login', [
            'login' => 'nonexistent@sipena.com',
            'password' => 'anypassword',
        ]);

        $response->assertSessionHasErrors([
            'login' => 'Login Gagal. Username salah.',
        ]);
    }
}
