<?php

namespace Tests\Feature;

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

class PasswordValidationTest extends TestCase
{
    use RefreshDatabase;

    private User $user;

    protected function setUp(): void
    {
        parent::setUp();

        $this->user = User::create([
            'msnv'        => 5001,
            'name'        => 'Test User',
            'mail'        => 'test@example.com',
            'pass'        => md5('password123'),
            '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'),
        ]);
    }

    public function test_password_change_requires_fields(): void
    {
        $res = $this->actingAs($this->user)
            ->post(route('user.update_password'), [
                'password' => '',
                'password_confirmation' => '',
            ]);

        $res->assertSessionHasErrors([
            'password' => 'Mật khẩu mới không được để trống.',
        ]);
    }

    public function test_password_change_requires_minimum_length(): void
    {
        $res = $this->actingAs($this->user)
            ->post(route('user.update_password'), [
                'password' => '123',
                'password_confirmation' => '123',
            ]);

        $res->assertSessionHasErrors([
            'password' => 'Mật khẩu mới phải có ít nhất 6 ký tự.',
        ]);
    }

    public function test_password_change_requires_confirmation_match(): void
    {
        $res = $this->actingAs($this->user)
            ->post(route('user.update_password'), [
                'password' => 'newpassword123',
                'password_confirmation' => 'differentpassword',
            ]);

        $res->assertSessionHasErrors([
            'password' => 'Mật khẩu xác nhận không trùng khớp.',
        ]);
    }
}
