<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\LoginModel;
use Illuminate\Support\Facades\DB;

class Testlogin extends TestCase
{
   

    /**
     * Test the login method of LoginModel.
     *
     * @return void
     */
    public function testLoginMethod()
    {
        // Simulate a user in the database
        $user = [
            'id_user' => 2,
            'username' => 'testuser',
            'password' => md5('testpassword'), // Use md5 hash for password
            'nama' => 'Test User',
            'email' => 'email',
            'id_user_level' => 2,
        ];

        DB::table('user')->insert($user);

        // Create an instance of LoginModel
        $loginModel = new LoginModel();

        // Test login with correct username and password
        $loggedInUser = $loginModel->login('testuser', md5('testpassword'));
        $this->assertNotNull($loggedInUser);
        $this->assertEquals('Test User', $loggedInUser->nama);
        $this->assertEquals('email', $loggedInUser->email);
        $wrongUser = $loginModel->login('wronguser', md5('wrongpassword'));
        $this->assertNull($wrongUser);
    }
}