<?php

namespace Tests\Feature;

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

class ManageSiteSettingsTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Guest users cannot access site builder settings.
     */
    public function test_guests_are_redirected_from_site_builder_settings()
    {
        $response = $this->get('/admin/manage-site-settings');

        // Filament auth middleware will redirect to admin login
        $response->assertRedirect('/admin/login');
    }

    /**
     * Non-admin users cannot access site builder settings.
     */
    public function test_customers_cannot_access_site_builder_settings()
    {
        $customer = User::factory()->create([
            'role' => 'customer',
        ]);

        $response = $this->actingAs($customer)->get('/admin/manage-site-settings');

        $response->assertStatus(403);
    }

    /**
     * Admin users can access the site builder settings page successfully.
     */
    public function test_admins_can_access_site_builder_settings()
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $response = $this->actingAs($admin)->get('/admin/manage-site-settings');
        file_put_contents(storage_path('logs/page_render2.html'), $response->getContent());

        $response->assertStatus(200);
        $response->assertSee('General Settings');
        $response->assertSee('Basic Information');
        $response->assertSee('Home Page Themes');
        $response->assertSee('Media');
        $response->assertSee('Seo');
        $response->assertSee('Site Meta Keywords');
        $response->assertSee('Site Meta Description');
    }

    /**
     * Test validation and save of site settings.
     */
    public function test_save_site_settings_validation()
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $this->actingAs($admin);

        \Livewire\Livewire::test(\App\Filament\Pages\ManageSiteSettings::class)
            ->call('save')
            ->assertHasNoErrors();
    }
}
