<?php

namespace Tests\Feature;

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

class EmployerProfileApiTest extends TestCase
{
    use RefreshDatabase;

    protected $employer;
    protected $token;

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

        $this->token = 'test-employer-token';
        $this->employer = User::create([
            'name' => 'Original Name',
            'email' => 'employer@example.com',
            'password' => bcrypt('Password@123'),
            'role' => 'employer',
            'api_token' => $this->token,
        ]);

        EmployerProfile::create([
            'user_id' => $this->employer->id,
            'company_name' => 'Original Company',
            'phone' => '+971501112222',
            'verification_status' => 'approved',
            'language' => 'English',
            'notifications' => true,
        ]);
    }

    /**
     * Test employer can retrieve profile.
     */
    public function test_employer_can_get_profile()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/profile');

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'data' => [
                    'profile' => [
                        'name',
                        'email',
                        'company_name',
                        'phone',
                        'language',
                        'notifications',
                        'verification_status',
                    ]
                ]
            ]);

        $this->assertEquals('Original Name', $response->json('data.profile.name'));
        $this->assertEquals('Original Company', $response->json('data.profile.company_name'));
    }

    /**
     * Test employer can update profile details.
     */
    public function test_employer_can_update_profile()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->postJson('/api/employers/profile/update', [
            'name' => 'Updated Name',
            'email' => 'employer_updated@example.com',
            'phone' => '+971509999999',
            'company_name' => 'Updated Company Corp',
            'language' => 'Arabic',
            'notifications' => false,
        ]);

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'message',
                'data' => [
                    'profile' => [
                        'name',
                        'email',
                        'company_name',
                        'phone',
                        'language',
                        'notifications',
                    ]
                ]
            ]);

        $this->assertDatabaseHas('users', [
            'id' => $this->employer->id,
            'name' => 'Updated Name',
            'email' => 'employer_updated@example.com',
        ]);

        $this->assertDatabaseHas('employer_profiles', [
            'user_id' => $this->employer->id,
            'company_name' => 'Updated Company Corp',
            'language' => 'Arabic',
            'notifications' => false,
        ]);
    }

    /**
     * Test employer can update password when providing correct current password.
     */
    public function test_employer_can_update_password()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->postJson('/api/employers/profile/update', [
            'name' => 'Original Name',
            'email' => 'employer@example.com',
            'phone' => '+971501112222',
            'company_name' => 'Original Company',
            'language' => 'English',
            'notifications' => true,
            'current_password' => 'Password@123',
            'new_password' => 'NewSecurePassword@123',
            'new_password_confirmation' => 'NewSecurePassword@123',
        ]);

        $response->assertStatus(200);

        // Verify password hash updated
        $this->assertTrue(Hash::check('NewSecurePassword@123', $this->employer->fresh()->password));
    }

    /**
     * Test employer password update fails with incorrect current password.
     */
    public function test_employer_password_update_fails_with_incorrect_current_password()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->postJson('/api/employers/profile/update', [
            'name' => 'Original Name',
            'email' => 'employer@example.com',
            'phone' => '+971501112222',
            'company_name' => 'Original Company',
            'language' => 'English',
            'notifications' => true,
            'current_password' => 'WrongCurrentPassword',
            'new_password' => 'NewSecurePassword@123',
            'new_password_confirmation' => 'NewSecurePassword@123',
        ]);

        $response->assertStatus(422)
            ->assertJsonStructure([
                'success',
                'message',
                'errors' => [
                    'current_password'
                ]
            ]);

        // Verify password was NOT changed
        $this->assertTrue(Hash::check('Password@123', $this->employer->fresh()->password));
    }
}
