<?php

namespace Tests\Feature\Tenant;

use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Tenant\Models\SocialMedia;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class TenantSocialMediaTest extends TestCase
{
    use RefreshDatabase;

    public function test_social_media_tables_have_the_expected_columns(): void
    {
        $this->assertEqualsCanonicalizing([
            'id',
            'code',
            'icon',
            'name',
            'created_at',
            'updated_at',
        ], Schema::getColumnListing('social_media'));

        $this->assertEqualsCanonicalizing([
            'id',
            'tenant_code',
            'social_media_code',
            'url',
            'orden',
            'created_at',
            'updated_at',
        ], Schema::getColumnListing('tenant_social_media'));
    }

    public function test_a_tenant_can_have_social_media_with_its_own_url(): void
    {
        $tenant = $this->createTenant();
        $instagram = SocialMedia::query()->create([
            'code' => 'instagram',
            'icon' => 'instagram',
            'name' => 'Instagram',
        ]);

        $tenant->socialMedia()->attach($instagram->code, [
            'url' => 'https://instagram.com/acme',
            'orden' => 3,
        ]);

        $this->assertTrue($tenant->socialMedia()->firstOrFail()->is($instagram));
        $this->assertSame(
            'https://instagram.com/acme',
            $tenant->socialMedia()->firstOrFail()->pivot->url
        );
        $this->assertSame(3, $tenant->socialMedia()->firstOrFail()->pivot->orden);
        $this->assertTrue($instagram->tenants()->firstOrFail()->is($tenant));
    }

    public function test_deleting_a_social_media_deletes_its_tenant_associations(): void
    {
        $tenant = $this->createTenant();
        $instagram = SocialMedia::query()->create([
            'code' => 'instagram',
            'icon' => 'instagram',
            'name' => 'Instagram',
        ]);
        $tenant->socialMedia()->attach($instagram->code, [
            'url' => 'https://instagram.com/acme',
        ]);

        $instagram->delete();

        $this->assertDatabaseMissing('tenant_social_media', [
            'tenant_code' => $tenant->codigo,
            'social_media_code' => $instagram->code,
        ]);
    }

    private function createTenant(): Tenant
    {
        $headerLogo = $this->createAttachment('header.png');
        $footerLogo = $this->createAttachment('footer.png');

        return Tenant::query()->create([
            'codigo' => 'acme',
            'nombre' => 'Acme',
            'dominio' => 'acme.com',
            'primary_color' => '#111111',
            'secondary_color' => '#222222',
            'danger_color' => '#333333',
            'success_color' => '#444444',
            'header_bg_color' => '#ffffff',
            'footer_bg_color' => '#ffffff',
            'header_logo_id' => $headerLogo->id,
            'footer_logo_id' => $footerLogo->id,
        ]);
    }

    private function createAttachment(string $filename): Attachment
    {
        return Attachment::query()->create([
            'path' => "test/{$filename}",
            'filename' => $filename,
            'type' => AttachmentType::Image,
            'mime_type' => 'image/png',
        ]);
    }

    private function createSocialMedia(string $code, string $name): SocialMedia
    {
        return SocialMedia::query()->create([
            'code' => $code,
            'icon' => $code,
            'name' => $name,
        ]);
    }
}
