<?php

namespace App\Domains\Tenant\Controllers\AdminApp;

use App\Domains\Auth\Models\User;
use App\Domains\Tenant\Resources\TenantResource;
use App\Domains\Tenant\Services\TenantInformationService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BootstrapTenantController extends Controller
{
    public function __construct(
        protected TenantInformationService $tenantInformationService
    ) {}

    public function __invoke(Request $request): TenantResource
    {
        /** @var User $user */
        $user = $request->user();
        $tenant = $user->tenant()->firstOrFail();

        return TenantResource::make(
            $this->tenantInformationService->load($tenant, [
                'menues' => fn ($query) => $query
                    ->where('code', 'like', 'admin.%')
                    ->whereHas(
                        'roles',
                        fn ($query) => $query->where('codigo', $user->rol_codigo)
                    ),
            ])
        );
    }
}
