<?php

namespace Tests\Feature;

use App\Models\PlatformSetting;
use App\Models\Role;
use App\Models\User;
use App\Models\UserType;
use App\Services\LandingPageContentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;

class LandingPageCmsTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    public function test_public_landing_page_renders_the_default_marketing_content(): void
    {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('Smart ERP for')
            ->assertSee('UAE Typing Centers')
            ->assertSee('Everything your typing center needs in one system')
            ->assertSee('Request a Demo');
    }

    public function test_super_admin_can_open_the_landing_page_cms(): void
    {
        $admin = User::where('email', 'admin@platform.com')->firstOrFail();

        $this->actingAs($admin)
            ->get(route('platform.landing.edit'))
            ->assertOk()
            ->assertSee('Landing Page CMS')
            ->assertSee('Save')
            ->assertSee('publish');
    }

    public function test_non_super_admin_platform_user_cannot_open_the_landing_page_cms(): void
    {
        $platformType = UserType::where('slug', 'platform_user')->firstOrFail();
        $platformRole = Role::where('slug', 'platform_admin')->firstOrFail();

        $platformAdmin = User::create([
            'user_type_id' => $platformType->id,
            'name' => 'Platform Staff Admin',
            'email' => 'platform.staff@example.test',
            'username' => 'platform.staff',
            'password' => Hash::make('Password123!'),
            'status' => 'active',
        ]);
        $platformAdmin->roles()->sync([$platformRole->id]);

        $this->actingAs($platformAdmin)
            ->get(route('platform.landing.edit'))
            ->assertForbidden();
    }

    public function test_tenant_user_cannot_access_the_landing_page_cms(): void
    {
        $tenantOwner = User::where('email', 'owner@almadina.ae')->firstOrFail();

        $this->actingAs($tenantOwner)
            ->get(route('platform.landing.edit'))
            ->assertForbidden();
    }

    public function test_super_admin_can_publish_content_that_immediately_appears_publicly(): void
    {
        $admin = User::where('email', 'admin@platform.com')->firstOrFail();
        $payload = app(LandingPageContentService::class)->defaults();
        $payload['hero']['title'] = 'Your smarter service center';
        $payload['hero']['highlight'] = 'Starts with GOVIX';
        $payload['footer']['privacy_url'] = '#';
        $payload['footer']['terms_url'] = '#';

        $this->actingAs($admin)
            ->put(route('platform.landing.update'), $payload)
            ->assertRedirect()
            ->assertSessionHas('status');

        $stored = json_decode(PlatformSetting::get(LandingPageContentService::SETTING_KEY), true);
        $this->assertSame('Your smarter service center', $stored['hero']['title']);
        $this->assertSame('Starts with GOVIX', $stored['hero']['highlight']);
        $this->assertDatabaseHas('audit_logs', [
            'user_id' => $admin->id,
            'module' => 'landing_page',
            'action' => 'landing_page.updated',
        ]);

        $this->get(route('home'))
            ->assertOk()
            ->assertSee('Your smarter service center')
            ->assertSee('Starts with GOVIX');
    }
}
