<?php

namespace Tests\Feature\Profile;

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

class ProfileAccountAccessTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_set_username_and_password_but_not_change_email_from_profile(): void
    {
        $user = User::factory()->create([
            'email' => 'candidate@example.com',
            'password_set_at' => null,
            'questionnaire_completed_at' => now(),
            'access_unlocked_at' => now(),
            'resume_analyzed_at' => now(),
        ]);

        $response = $this->actingAs($user)->post(route('profile.update'), [
            'email' => 'changed@example.com',
            'username' => 'Remote_User',
            'new_password' => 'NewSecret123',
            'new_password_confirmation' => 'NewSecret123',
            'headline' => 'Remote support specialist',
            'summary' => 'Experienced support specialist.',
            'current_role' => 'Support specialist',
            'experience_level' => 'mid',
            'country_code' => 'IN',
            'city' => 'Kolkata',
            'timezone' => 'Asia/Kolkata',
            'skills' => ['Customer support', 'CRM'],
            'preferred_roles' => ['Customer Support Specialist'],
            'preferred_categories' => ['support'],
            'links' => [],
            'availability' => 'immediate',
            'salary_currency' => 'USD',
            'salary_min' => 3000,
            'open_to_contract' => true,
            'open_to_full_time' => true,
        ]);

        $response->assertRedirect(route('applicant.dashboard'));

        $user->refresh();

        $this->assertSame('candidate@example.com', $user->email);
        $this->assertSame('remote_user', $user->username);
        $this->assertTrue(Hash::check('NewSecret123', $user->password));
        $this->assertNotNull($user->password_set_at);
    }
}
