<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Livewire\Livewire;
use Filament\Facades\Filament;

class AdminThemeBrandingTest extends TestCase
{
    use RefreshDatabase;

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

        // Establish admin panel context for Livewire tests
        Filament::setCurrentPanel(Filament::getPanel('admin'));
    }

    /**
     * Admin can save theme preset settings successfully.
     */
    public function test_admin_can_save_theme_preset_settings()
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($admin);
        Livewire::actingAs($admin);

        Livewire::test(\App\Filament\Pages\AdminBranding::class)
            ->fillForm([
                'theme_preset' => 'emerald',
            ])
            ->call('save')
            ->assertHasNoFormErrors();

        $this->assertEquals('emerald', Setting::getValue('theme_preset'));
    }

    /**
     * Admin can save new presets (teal and orange) successfully.
     */
    public function test_admin_can_save_new_presets_teal_and_orange()
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($admin);
        Livewire::actingAs($admin);

        Livewire::test(\App\Filament\Pages\AdminBranding::class)
            ->fillForm([
                'theme_preset' => 'teal',
            ])
            ->call('save')
            ->assertHasNoFormErrors();

        $this->assertEquals('teal', Setting::getValue('theme_preset'));

        Livewire::test(\App\Filament\Pages\AdminBranding::class)
            ->fillForm([
                'theme_preset' => 'orange',
            ])
            ->call('save')
            ->assertHasNoFormErrors();

        $this->assertEquals('orange', Setting::getValue('theme_preset'));
    }

    /**
     * Admin can save custom theme color settings successfully.
     */
    public function test_admin_can_save_custom_theme_color_settings()
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($admin);
        Livewire::actingAs($admin);

        Livewire::test(\App\Filament\Pages\AdminBranding::class)
            ->fillForm([
                'theme_preset' => 'custom',
                'primary_color' => '#123456',
            ])
            ->call('save')
            ->assertHasNoFormErrors();

        $this->assertEquals('custom', Setting::getValue('theme_preset'));
        $this->assertEquals('#123456', Setting::getValue('primary_color'));
    }

    public function test_user_can_update_profile_phone_and_birthday()
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($admin);
        Livewire::actingAs($admin);

        \Illuminate\Support\Facades\Storage::fake('public');
        $avatar = \Illuminate\Http\UploadedFile::fake()->image('avatar.png');

        Livewire::test(\App\Filament\Pages\Auth\EditProfile::class)
            ->fillForm([
                'name' => 'Updated Name',
                'email' => $admin->email,
                'avatar' => $avatar,
                'phone' => '01712345678',
                'birthday' => '1995-10-20',
            ])
            ->call('save')
            ->assertHasNoFormErrors();

        $admin->refresh();
        $this->assertEquals('Updated Name', $admin->name);
        $this->assertEquals('01712345678', $admin->phone);
        $this->assertEquals('1995-10-20', $admin->birthday?->format('Y-m-d'));
        $this->assertNotNull($admin->avatar);
    }

    /**
     * Database notifications are fully operational and can be dispatched/persisted.
     */
    public function test_database_notifications_can_be_dispatched_and_persisted()
    {
        $user = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($user);

        // Dispatch a database notification
        \Filament\Notifications\Notification::make()
            ->title('Test Notification')
            ->body('This is a test notification body')
            ->success()
            ->sendToDatabase($user);

        // Verify it was saved in database
        $this->assertDatabaseHas('notifications', [
            'notifiable_id' => $user->id,
            'notifiable_type' => User::class,
        ]);

        $notification = $user->notifications()->first();
        $this->assertEquals('Test Notification', $notification->data['title']);
        $this->assertEquals('This is a test notification body', $notification->data['body']);
    }
}
