<?php

namespace Tests\Feature\Menu;

use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Menu\Models\Menu;
use App\Domains\Tenant\Models\Tenant;
use Database\Seeders\MenuSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TenantMenuControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_validates_static_content_with_the_menu_schema(): void
    {
        $tenant = $this->createTenant();
        $menu = Menu::query()->create([
            'code' => 'about',
            'label' => 'Nosotros',
            'content_type' => Menu::CONTENT_TYPE_STATIC,
            'static_content_schema' => [
                'title' => 'required|string|max:20',
                'sections' => 'required|array|min:1',
                'sections.*.body' => 'required|string',
            ],
            'route' => '/about',
        ]);

        $this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}", [
            'static_content' => [
                'title' => 123,
                'sections' => [],
            ],
        ])
            ->assertUnprocessable()
            ->assertJsonValidationErrors([
                'static_content.title',
                'static_content.sections',
            ]);

        $this->assertDatabaseMissing('tenants_menues', [
            'tenant_code' => $tenant->codigo,
            'menu_code' => $menu->code,
        ]);
    }

    public function test_it_configures_a_static_menu_when_content_matches_the_schema(): void
    {
        $tenant = $this->createTenant();
        $menu = Menu::query()->create([
            'code' => 'about',
            'label' => 'Nosotros',
            'content_type' => Menu::CONTENT_TYPE_STATIC,
            'static_content_schema' => [
                'title' => 'required|string|max:20',
                'sections' => 'required|array|min:1',
                'sections.*.body' => 'required|string',
            ],
            'route' => '/about',
        ]);

        $content = [
            'title' => 'Nosotros',
            'sections' => [
                ['body' => 'Nuestra historia'],
            ],
        ];

        $this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}", [
            'static_content' => $content,
        ])
            ->assertOk()
            ->assertJsonPath('static_content.title', 'Nosotros')
            ->assertJsonPath('static_content.sections.0.body', 'Nuestra historia');

        $this->assertSame(
            $content,
            $tenant->menues()->where('menues.code', $menu->code)->firstOrFail()->pivot->static_content
        );
    }

    public function test_dynamic_menus_do_not_require_static_content(): void
    {
        $tenant = $this->createTenant();
        $menu = Menu::query()->create([
            'code' => 'catalog',
            'label' => 'Catálogo',
            'route' => '/catalog',
        ]);

        $this->postJson("/api/{$tenant->codigo}/menues/{$menu->code}")
            ->assertOk()
            ->assertJsonPath('static_content', null);

        $this->assertDatabaseHas('tenants_menues', [
            'tenant_code' => $tenant->codigo,
            'menu_code' => $menu->code,
            'static_content' => null,
        ]);
    }

    public function test_contact_content_requires_coordinates_inside_each_address(): void
    {
        $tenant = $this->createTenant();
        $this->seed(MenuSeeder::class);

        $this->postJson("/api/{$tenant->codigo}/menues/help.contact", [
            'static_content' => [
                'whatsapp' => [
                    'whatsapp_url' => 'https://wa.me/543412602222',
                    'whatsapp_label' => 'Chatea con nosotros',
                ],
                'phone' => '+54 9 (0341) 6658247',
                'locations' => [
                    'rosario' => [
                        'label' => 'Rosario',
                        'addresses' => [
                            [
                                'label' => 'Av. San Lorenzo 1542',
                                'address' => 'Av. San Lorenzo 1542, Rosario',
                                'coordinates' => [-91, -181],
                            ],
                        ],
                    ],
                ],
                'map_locations' => [
                    'legacy_point' => [-32.946820, -60.639320],
                ],
            ],
        ])
            ->assertUnprocessable()
            ->assertJsonValidationErrors([
                'static_content.locations.rosario.addresses.0.coordinates.0',
                'static_content.locations.rosario.addresses.0.coordinates.1',
                'static_content.map_locations',
            ]);
    }

    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',
        ]);
    }
}
