<?php

namespace Tests\Feature\Migrations;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class SetWebsiteTypeFaviconTest extends TestCase
{
    use RefreshDatabase;

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

        DB::table('website_type')->insert([
            [
                'codigo' => 'shopit',
                'nombre' => 'ShopIt',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'codigo' => 'onticket',
                'nombre' => 'OnTicket',
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        $migration = require database_path(
            'migrations/2026_08_26_000000_set_website_type_favicon.php'
        );

        $migration->up();
        $migration->up();

        $faviconIds = DB::table('website_type')
            ->whereIn('codigo', ['shopit', 'onticket'])
            ->pluck('favicon_id');

        $this->assertCount(2, $faviconIds);
        $this->assertNotNull($faviconIds->first());
        $this->assertSame(1, $faviconIds->unique()->count());

        $attachment = DB::table('attachments')->where('id', $faviconIds->first())->first();

        $this->assertNotNull($attachment);
        $this->assertSame('onticket_favicon.svg', $attachment->filename);
        $this->assertSame('image/svg+xml', $attachment->mime_type);
        $this->assertSame('svg', $attachment->extension);
        $this->assertSame(1, DB::table('attachments')->where('filename', 'onticket_favicon.svg')->count());
        Storage::disk('s3')->assertExists($attachment->path);
        $this->assertSame(
            file_get_contents(public_path('images/website_types/onticket_favicon.svg')),
            Storage::disk('s3')->get($attachment->path),
        );
    }
}
