<?php

namespace Tests\Unit;

use App\Services\Settings\SettingService;
use App\Support\HomepageAssets;
use App\Support\HomepageSettings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class HomepageSettingsTest extends TestCase
{
    use RefreshDatabase;
    public function test_sanitize_html_strips_unsafe_tags(): void
    {
        $html = '<p>Hello</p><script>alert(1)</script><strong>World</strong>';

        $this->assertSame('<p>Hello</p><strong>World</strong>', HomepageSettings::sanitizeHtml($html));
    }

    public function test_plain_text_strips_tags(): void
    {
        $html = '<p>Hello <strong>World</strong></p>';

        $this->assertSame('Hello World', HomepageSettings::plainText($html));
    }

    public function test_hero_image_url_is_null_when_missing(): void
    {
        $this->assertNull(HomepageSettings::heroImageUrl(null));
        $this->assertNull(HomepageSettings::heroImageUrl(''));
    }

    public function test_resolve_includes_hero_image_url_when_stored(): void
    {
        Storage::fake('public');

        $path = UploadedFile::fake()->image('hero.jpg')->store(HomepageAssets::HERO_DIRECTORY, HomepageAssets::DISK);

        app(SettingService::class)->updateMany([
            'homepage.hero_image' => ['value' => $path, 'category' => 'homepage', 'type' => 'string'],
        ]);

        $content = HomepageSettings::resolve();

        $this->assertSame($path, $content['hero_image']);
        $this->assertNotNull($content['hero_image_url']);
    }
}
