<?php

use App\Models\User;
use Livewire\Volt\Volt;

// Authorization Tests
test('client can view individual profile page', function () {
    $user = User::factory()->individual()->create();

    $this->actingAs($user)
        ->get(route('client.profile'))
        ->assertOk()
        ->assertSeeLivewire('client.profile');
});

test('company client can view profile page', function () {
    $user = User::factory()->company()->create();

    $this->actingAs($user)
        ->get(route('client.profile'))
        ->assertOk()
        ->assertSeeLivewire('client.profile');
});

test('unauthenticated users cannot access profile', function () {
    $this->get(route('client.profile'))
        ->assertRedirect(route('login'));
});

test('admin cannot access client profile', function () {
    $admin = User::factory()->admin()->create();

    $this->actingAs($admin)
        ->get(route('client.profile'))
        ->assertForbidden();
});

// Individual Profile Display Tests
test('individual profile displays all required fields', function () {
    $user = User::factory()->individual()->create([
        'full_name' => 'Test User',
        'national_id' => '123456789',
        'email' => 'test@example.com',
        'phone' => '+970599999999',
        'preferred_language' => 'en',
    ]);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee('Test User')
        ->assertSee('123456789')
        ->assertSee('test@example.com')
        ->assertSee('+970599999999')
        ->assertSee(__('profile.english'));
});

test('individual profile displays full name prominently', function () {
    $user = User::factory()->individual()->create(['full_name' => 'John Smith']);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee('John Smith');
});

test('individual profile displays national id', function () {
    $user = User::factory()->individual()->create(['national_id' => '987654321']);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee('987654321');
});

test('individual profile shows arabic language when preferred', function () {
    $user = User::factory()->individual()->create(['preferred_language' => 'ar']);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee(__('profile.arabic'));
});

// Company Profile Display Tests
test('company profile displays all required fields', function () {
    $user = User::factory()->company()->create([
        'company_name' => 'Test Company',
        'company_cert_number' => 'REG-12345',
        'contact_person_name' => 'John Doe',
        'contact_person_id' => '987654321',
        'email' => 'company@example.com',
        'phone' => '+970599888888',
        'preferred_language' => 'ar',
    ]);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee('Test Company')
        ->assertSee('REG-12345')
        ->assertSee('John Doe')
        ->assertSee('987654321')
        ->assertSee('company@example.com')
        ->assertSee('+970599888888');
});

test('company profile displays company name prominently', function () {
    $user = User::factory()->company()->create(['company_name' => 'ABC Corporation']);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee('ABC Corporation');
});

test('company profile displays registration number', function () {
    $user = User::factory()->company()->create(['company_cert_number' => 'CR-99999']);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee('CR-99999');
});

test('company profile displays contact person details', function () {
    $user = User::factory()->company()->create([
        'contact_person_name' => 'Jane Smith',
        'contact_person_id' => '111222333',
    ]);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee('Jane Smith')
        ->assertSee('111222333');
});

// Account Type Badge Tests
test('profile page shows correct account type badge for individual', function () {
    $user = User::factory()->individual()->create();

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee(__('profile.individual_account'));
});

test('profile page shows correct account type badge for company', function () {
    $user = User::factory()->company()->create();

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee(__('profile.company_account'));
});

// Read-Only Profile Tests
test('profile page has no edit functionality', function () {
    $user = User::factory()->individual()->create();

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertDontSee('wire:model');
});

test('profile page does not have edit button', function () {
    $user = User::factory()->individual()->create();

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertDontSee('Edit')
        ->assertDontSee('Update');
});

// Contact Admin Message Tests
test('profile page shows contact admin message', function () {
    $user = User::factory()->individual()->create();

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee(__('client.contact_admin_to_update'));
});

// Logout Tests
test('logout button logs out user and redirects to login', function () {
    $user = User::factory()->individual()->create();

    $this->actingAs($user);

    Volt::test('client.profile')
        ->call('logout')
        ->assertRedirect(route('login'));

    $this->assertGuest();
});

test('logout button is visible on profile page', function () {
    $user = User::factory()->individual()->create();

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee(__('auth.logout'));
});

// Member Since Date Tests
test('profile displays member since date', function () {
    $user = User::factory()->individual()->create([
        'created_at' => now()->subYear(),
    ]);

    $this->actingAs($user);

    Volt::test('client.profile')
        ->assertSee(__('profile.member_since'));
});
