<?php

namespace Tests\Tenant;

use App\Facades\Feature;
use App\Facades\Settings;
use App\Http\Resources\SiteResource;
use Illuminate\Support\Facades\Storage;
use Tests\TenantTestCase;

class SiteResourceStaticFilesTest extends TenantTestCase
{
    private function setSplit(bool $enabled): void
    {
        Settings::make([
            'tab' => 'SEO',
            'section' => 'Sitemap',
            'group' => 'General',
            'name' => 'Split sitemap',
            'config_key' => 'seo-sitemap-split',
            'value' => $enabled,
            'type' => 'boolean',
        ]);
    }

    public function testStaticFilesEmptyWhenSitemapFeatureDisabled(): void
    {
        Storage::fake('file-storage');
        Storage::disk('file-storage')->put(tenant('id') . '/sitemap.xml', '<xml />');

        $resource = new SiteResource([])->toArray(request());

        $this->assertArrayHasKey('static_files', $resource);
        $this->assertSame([], $resource['static_files']);
    }

    public function testStaticFilesListsSingleSitemap(): void
    {
        Feature::setEnabled(tenant(), ['sitemap']);
        $this->setSplit(false);
        Storage::fake('file-storage');
        Storage::disk('file-storage')->put(tenant('id') . '/sitemap.xml', '<xml />');

        $resource = new SiteResource([])->toArray(request());

        $this->assertCount(1, $resource['static_files']);
        $this->assertSame('url', $resource['static_files'][0]['type']);
        $this->assertStringContainsString('sitemap.xml', $resource['static_files'][0]['value']);
        $this->assertStringContainsString('?t=', $resource['static_files'][0]['value']);
    }

    public function testStaticFilesListsIndexAndAllSectionFiles(): void
    {
        Feature::setEnabled(tenant(), ['sitemap']);
        $this->setSplit(true);
        Storage::fake('file-storage');
        $disk = Storage::disk('file-storage');
        $disk->put(tenant('id') . '/sitemap-index.xml', '<xml />');
        $disk->put(tenant('id') . '/sitemap-pages.xml', '<xml />');
        $disk->put(tenant('id') . '/sitemap-vehicles.xml', '<xml />');

        $resource = new SiteResource([])->toArray(request());

        $this->assertCount(3, $resource['static_files']);

        foreach ($resource['static_files'] as $file) {
            $this->assertSame('url', $file['type']);
            $this->assertStringContainsString('?t=', $file['value']);
        }

        $values = array_column($resource['static_files'], 'value');
        $this->assertStringContainsString('sitemap-index.xml', $values[0]);

        $haystack = implode(' ', $values);
        $this->assertStringContainsString('sitemap-pages.xml', $haystack);
        $this->assertStringContainsString('sitemap-vehicles.xml', $haystack);
    }

    public function testStaticFilesIgnoresNonSitemapFiles(): void
    {
        Feature::setEnabled(tenant(), ['sitemap']);
        $this->setSplit(true);
        Storage::fake('file-storage');
        $disk = Storage::disk('file-storage');
        $disk->put(tenant('id') . '/sitemap-index.xml', '<xml />');
        $disk->put(tenant('id') . '/some-other-file.xml', '<xml />');
        $disk->put(tenant('id') . '/logo.png', 'binary');

        $resource = new SiteResource([])->toArray(request());

        $this->assertCount(1, $resource['static_files']);
        $this->assertStringContainsString('sitemap-index.xml', $resource['static_files'][0]['value']);
    }
}
