<?php

namespace Tests\Feature\Tenant;

use App\Domains\Tenant\Services\WebsiteTypeService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class WebsiteTypeServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_creates_a_website_type_and_uploads_its_logos_to_s3(): void
    {
        Storage::fake('s3');

        $websiteType = app(WebsiteTypeService::class)->create([
            'codigo' => 'marketplace',
            'nombre' => 'Marketplace',
            'dominio' => 'marketplace.test',
            'scanner_domain' => 'scanner.marketplace.test',
            'primary_color' => '#111111',
            'secondary_color' => '#222222',
            'danger_color' => '#ff0000',
            'success_color' => '#00aa55',
            'warning_color' => '#ffaa00',
            'body_color' => '#666666',
            'darker_body_color' => '#333333',
            'surface_color' => '#ffffff',
            'background_color' => '#f8f8f8',
            'border_color' => '#dddddd',
            'login_header_footer_color' => '#333333',
            'site_title' => 'Marketplace Central',
            'site_logo' => UploadedFile::fake()->image('site-logo.png'),
            'footer_logo' => UploadedFile::fake()->image('footer-logo.png'),
            'favicon' => UploadedFile::fake()->image('favicon.png', 32, 32),
        ]);

        $siteLogo = $websiteType->siteLogo()->firstOrFail();
        $footerLogo = $websiteType->footerLogo()->firstOrFail();
        $favicon = $websiteType->favicon()->firstOrFail();

        $this->assertSame('marketplace', $websiteType->codigo);
        $this->assertSame($siteLogo->id, $websiteType->site_logo);
        $this->assertSame($footerLogo->id, $websiteType->footer_logo);
        $this->assertSame($favicon->id, $websiteType->favicon_id);
        $this->assertStringStartsWith('website-types/', $siteLogo->path);
        $this->assertStringStartsWith('website-types/', $footerLogo->path);
        $this->assertStringStartsWith('website-types/', $favicon->path);
        Storage::disk('s3')->assertExists($siteLogo->path);
        Storage::disk('s3')->assertExists($footerLogo->path);
        Storage::disk('s3')->assertExists($favicon->path);
        $this->assertDatabaseHas('website_type', [
            'codigo' => 'marketplace',
            'dominio' => 'marketplace.test',
            'scanner_domain' => 'scanner.marketplace.test',
            'warning_color' => '#ffaa00',
            'body_color' => '#666666',
            'darker_body_color' => '#333333',
            'surface_color' => '#ffffff',
            'background_color' => '#f8f8f8',
            'border_color' => '#dddddd',
            'login_header_footer_color' => '#333333',
            'site_title' => 'Marketplace Central',
            'site_logo' => $siteLogo->id,
            'footer_logo' => $footerLogo->id,
            'favicon_id' => $favicon->id,
        ]);
    }

    public function test_it_keeps_a_shared_favicon_when_one_website_type_replaces_it(): void
    {
        Storage::fake('s3');

        $service = app(WebsiteTypeService::class);
        $shopIt = $service->create([
            'codigo' => 'shopit',
            'nombre' => 'ShopIt',
            'favicon' => UploadedFile::fake()->image('shared-favicon.png', 32, 32),
        ]);
        $sharedFavicon = $shopIt->favicon()->firstOrFail();
        $onTicket = $service->create([
            'codigo' => 'onticket',
            'nombre' => 'OnTicket',
            'favicon' => $sharedFavicon->key,
        ]);

        $service->updateOrCreate(
            ['codigo' => 'shopit'],
            ['favicon' => UploadedFile::fake()->image('shopit-favicon.png', 32, 32)],
        );

        $this->assertSame($sharedFavicon->id, $onTicket->fresh()->favicon_id);
        $this->assertDatabaseHas('attachments', ['id' => $sharedFavicon->id]);
        Storage::disk('s3')->assertExists($sharedFavicon->path);
    }
}
