<?php

namespace Tests\Feature;

use App\Models\User;
use App\Services\Admin\Contracts\AdminServiceInterface;
use App\Services\User\Contracts\UserServiceInterface;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PasswordMd5Test extends TestCase
{
    use RefreshDatabase;

    private UserServiceInterface $userService;
    private AdminServiceInterface $adminService;

    protected function setUp(): void
    {
        parent::setUp();
        $this->userService = $this->app->make(UserServiceInterface::class);
        $this->adminService = $this->app->make(AdminServiceInterface::class);
    }

    public function test_user_service_update_password_stores_as_md5(): void
    {
        $user = User::create([
            'msnv'        => 4001,
            'name'        => 'Test User',
            'mail'        => 'test@example.com',
            'pass'        => md5('old_password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_TRUE'),
        ]);

        $this->userService->updatePassword($user, 'new_secret_password');

        $user->refresh();

        $this->assertEquals(md5('new_secret_password'), $user->pass);
        $this->assertEquals(config('constants.FIRST_LOGIN_FALSE'), $user->first_login);
    }

    public function test_admin_service_create_user_stores_password_as_md5(): void
    {
        $data = [
            'msnv'        => 4002,
            'name'        => 'Created User',
            'mail'        => 'created@example.com',
            'departments' => config('constants.DEPARTMENTS')[1],
            'password'    => 'admin_created_pass',
            'role'        => config('constants.ROLE_MEMBER'),
        ];

        $this->adminService->createUser($data);

        $user = User::where('msnv', 4002)->first();

        $this->assertNotNull($user);
        $this->assertEquals(md5('admin_created_pass'), $user->pass);
        $this->assertEquals(config('constants.FIRST_LOGIN_TRUE'), $user->first_login);
    }
}
