<?php

namespace Tests\Feature\Tenant;

use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Authorization\Models\Role;
use App\Domains\Menu\Models\Menu;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class AdminAppBootstrapTenantControllerTest extends TestCase
{
    use RefreshDatabase;

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

    public function test_a_customer_cannot_bootstrap_adminapp(): void
    {
        $userRole = $this->createRole(RoleCode::User);
        $customer = User::factory()->create([
            'rol_codigo' => $userRole->codigo,
            'tenant_codigo' => null,
        ]);

        Sanctum::actingAs($customer);

        $this->getJson('/api/v1/adminapp/tenant/bootstrap')
            ->assertForbidden();
    }

    public function test_it_returns_the_authenticated_users_tenant_and_admin_menus(): void
    {
        $adminAppRole = $this->createRole(RoleCode::AdminApp);
        $tenant = $this->createTenant('acme');
        $otherTenant = $this->createTenant('other');

        $catalog = $this->createMenu('admin.catalog', 'Catálogo', '/admin/catalog');
        $staff = $this->createMenu('admin.staff', 'Staff', '/admin/staff');
        $storefront = $this->createMenu('index', 'Inicio', '/');

        $adminAppRole->menus()->sync([$catalog->code, $storefront->code]);
        $tenant->menues()->sync([$catalog->code, $staff->code, $storefront->code]);
        $otherTenant->menues()->sync([$staff->code]);

        Sanctum::actingAs(User::factory()->create([
            'rol_codigo' => $adminAppRole->codigo,
            'tenant_codigo' => $tenant->codigo,
        ]));

        $this->getJson('/api/v1/adminapp/tenant/bootstrap')
            ->assertOk()
            ->assertJsonPath('data.codigo', 'acme')
            ->assertJsonCount(1, 'data.menues')
            ->assertJsonPath('data.menues.0.code', 'admin.catalog')
            ->assertJsonPath('data.menues.0.label', 'Catálogo')
            ->assertJsonPath('data.menues.0.route', '/admin/catalog')
            ->assertJsonMissing(['code' => 'admin.staff'])
            ->assertJsonMissing(['code' => 'index']);
    }

    private function createRole(RoleCode $role): Role
    {
        return Role::query()->create([
            'codigo' => $role->value,
            'nombre' => $role->value,
        ]);
    }

    private function createTenant(string $code): Tenant
    {
        return Tenant::query()->create([
            'codigo' => $code,
            'nombre' => ucfirst($code),
            'dominio' => "{$code}.test",
        ]);
    }

    private function createMenu(string $code, string $label, string $route): Menu
    {
        return Menu::query()->create([
            'code' => $code,
            'label' => $label,
            'route' => $route,
        ]);
    }
}
