<?php

use App\Mail\TestEmail;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Livewire\Volt\Volt;

test('profile page is displayed', function () {
    $this->actingAs($user = User::factory()->create());

    $this->get(route('profile.edit'))->assertOk();
});

test('profile information can be updated', function () {
    $user = User::factory()->create();

    $this->actingAs($user);

    $response = Volt::test('settings.profile')
        ->set('full_name', 'Test User')
        ->set('email', 'test@example.com')
        ->set('preferred_language', 'en')
        ->call('updateProfileInformation');

    $response->assertHasNoErrors();

    $user->refresh();

    expect($user->full_name)->toEqual('Test User');
    expect($user->email)->toEqual('test@example.com');
    expect($user->preferred_language)->toEqual('en');
    expect($user->email_verified_at)->toBeNull();
});

test('email verification status is unchanged when email address is unchanged', function () {
    $user = User::factory()->create();

    $this->actingAs($user);

    $response = Volt::test('settings.profile')
        ->set('full_name', 'Test User')
        ->set('email', $user->email)
        ->call('updateProfileInformation');

    $response->assertHasNoErrors();

    expect($user->refresh()->email_verified_at)->not->toBeNull();
});

// ===========================================
// Preferred Language Tests
// ===========================================

test('user can update preferred language', function () {
    $user = User::factory()->create(['preferred_language' => 'ar']);

    $this->actingAs($user);

    Volt::test('settings.profile')
        ->set('preferred_language', 'en')
        ->call('updateProfileInformation')
        ->assertHasNoErrors();

    expect($user->fresh()->preferred_language)->toBe('en');
});

test('preferred language validates allowed values', function () {
    $user = User::factory()->create();

    $this->actingAs($user);

    Volt::test('settings.profile')
        ->set('preferred_language', 'fr')
        ->call('updateProfileInformation')
        ->assertHasErrors(['preferred_language']);
});

test('component initializes with user preferred language', function () {
    $user = User::factory()->create(['preferred_language' => 'en']);

    $this->actingAs($user);

    $component = Volt::test('settings.profile');

    expect($component->get('preferred_language'))->toBe('en');
});

// ===========================================
// Test Email Tests (Admin Only)
// ===========================================

test('admin can send test email', function () {
    Mail::fake();

    $admin = User::factory()->admin()->create();

    $this->actingAs($admin);

    Volt::test('settings.profile')
        ->call('sendTestEmail')
        ->assertHasNoErrors();

    Mail::assertSent(TestEmail::class, fn ($mail) => $mail->hasTo($admin->email));
});

test('test email uses admin preferred language', function () {
    Mail::fake();

    $admin = User::factory()->admin()->create(['preferred_language' => 'ar']);

    $this->actingAs($admin);

    Volt::test('settings.profile')
        ->call('sendTestEmail');

    Mail::assertSent(TestEmail::class, fn ($mail) => $mail->locale === 'ar');
});

test('non-admin cannot send test email', function () {
    Mail::fake();

    $client = User::factory()->individual()->create();

    $this->actingAs($client);

    Volt::test('settings.profile')
        ->call('sendTestEmail');

    Mail::assertNothingSent();
});

test('admin sees email configuration section', function () {
    $admin = User::factory()->admin()->create();

    $this->actingAs($admin)
        ->get(route('profile.edit'))
        ->assertOk()
        ->assertSee(config('mail.from.name'))
        ->assertSee(config('mail.from.address'));
});

test('user can delete their account', function () {
    $user = User::factory()->create();

    $this->actingAs($user);

    $response = Volt::test('settings.delete-user-form')
        ->set('password', 'password')
        ->call('deleteUser');

    $response
        ->assertHasNoErrors()
        ->assertRedirect('/');

    expect($user->fresh())->toBeNull();
    expect(auth()->check())->toBeFalse();
});

test('correct password must be provided to delete account', function () {
    $user = User::factory()->create();

    $this->actingAs($user);

    $response = Volt::test('settings.delete-user-form')
        ->set('password', 'wrong-password')
        ->call('deleteUser');

    $response->assertHasErrors(['password']);

    expect($user->fresh())->not->toBeNull();
});
