<?php

namespace Tests\Feature\Tenant;

use App\Domains\Attachable\Models\Attachment;
use App\Domains\Tenant\Models\WebsiteType;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class BootstrapAdminAppControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_publicly_bootstraps_the_admin_app_by_domain(): void
    {
        $footerLogo = Attachment::factory()->create();
        $favicon = Attachment::factory()->create();

        WebsiteType::query()->create([
            'codigo' => 'shopit',
            'nombre' => 'ShopIt',
            'dominio' => 'admin.shopit.test',
            'primary_color' => '#112233',
            'secondary_color' => '#445566',
            'danger_color' => '#aa0000',
            'success_color' => '#00aa00',
            'warning_color' => '#ffaa00',
            'body_color' => '#666666',
            'darker_body_color' => '#333333',
            'surface_color' => '#ffffff',
            'background_color' => '#f8f8f8',
            'border_color' => '#eaeaea',
            'login_header_footer_color' => '#313131',
            'footer_logo' => $footerLogo->id,
            'favicon_id' => $favicon->id,
        ]);

        $this->getJson('/api/v1/adminapp/bootstrap/ADMIN.SHOPIT.TEST')
            ->assertOk()
            ->assertJsonPath('data.website_type_code', 'shopit')
            ->assertJsonPath('data.primary_color', '#112233')
            ->assertJsonPath('data.warning_color', '#ffaa00')
            ->assertJsonPath('data.login_header_footer_color', '#313131')
            ->assertJsonPath('data.site_logo', null)
            ->assertJsonPath('data.footer_logo', $footerLogo->getTemporaryUrl(1440))
            ->assertJsonPath('data.favicon', $favicon->getTemporaryUrl(1440))
            ->assertJsonMissingPath('data.forms')
            ->assertJsonMissingPath('data.codigo')
            ->assertJsonMissingPath('data.nombre')
            ->assertJsonMissingPath('data.dominio');
    }

    public function test_it_returns_not_found_for_an_unknown_domain(): void
    {
        $this->getJson('/api/v1/adminapp/bootstrap/unknown.test')
            ->assertNotFound();
    }

    public function test_it_rejects_an_invalid_domain(): void
    {
        $invalidDomain = str_repeat('a', 256);

        $this->getJson("/api/v1/adminapp/bootstrap/{$invalidDomain}")
            ->assertUnprocessable()
            ->assertJsonValidationErrors(['dominio']);
    }
}
