<?php

namespace Tests\Feature\Forms;

use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use Database\Seeders\AuthorizationSeeder;
use Database\Seeders\SocialMediaSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class AdminAppEventFormControllerTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        $this->seed([AuthorizationSeeder::class, SocialMediaSeeder::class]);
        WebsiteType::query()->create([
            'codigo' => 'onticket',
            'nombre' => 'OnTicket',
        ]);
    }

    public function test_authentication_is_required(): void
    {
        $this->getJson('/api/v1/adminapp/forms/event')->assertUnauthorized();
    }

    public function test_an_adminapp_user_can_get_the_event_form(): void
    {
        $tenant = Tenant::query()->create([
            'codigo' => 'acme',
            'nombre' => 'Acme',
            'dominio' => 'acme.test',
            'website_type_code' => 'onticket',
        ]);
        $tenant->socialMedia()->attach('instagram', [
            'url' => 'https://instagram.com/acme',
            'orden' => 1,
        ]);
        Sanctum::actingAs(User::factory()->create([
            'rol_codigo' => RoleCode::AdminApp->value,
            'tenant_codigo' => $tenant->codigo,
        ]));

        $this->getJson('/api/v1/adminapp/forms/event')
            ->assertOk()
            ->assertJsonCount(4, 'data.social_media')
            ->assertJsonPath('data.social_media.0.code', 'facebook')
            ->assertJsonPath('data.social_media.0.name', 'Facebook')
            ->assertJsonPath('data.social_media.0.icon', 'fa-brands fa-facebook')
            ->assertJsonPath('data.social_media.0.url', null)
            ->assertJsonPath('data.social_media.1.code', 'instagram')
            ->assertJsonPath('data.social_media.1.url', 'https://instagram.com/acme')
            ->assertJsonPath('data.social_media.3.code', 'linkedin');
    }

    public function test_a_customer_cannot_get_the_event_form(): void
    {
        Sanctum::actingAs(User::factory()->create([
            'rol_codigo' => RoleCode::User->value,
            'tenant_codigo' => null,
        ]));

        $this->getJson('/api/v1/adminapp/forms/event')->assertForbidden();
    }
}
