<?php

namespace App\Domains\Menu\Services;

use App\Domains\Menu\Models\Menu;
use App\Domains\Menu\Models\TenantMenu;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Support\Facades\DB;

class TenantMenuService
{
    public function configure(Tenant $tenant, Menu $menu, ?array $staticContent): TenantMenu
    {
        return DB::transaction(fn () => TenantMenu::query()->updateOrCreate(
            [
                'tenant_code' => $tenant->codigo,
                'menu_code' => $menu->code,
            ],
            [
                'static_content' => $menu->content_type === Menu::CONTENT_TYPE_STATIC
                    ? $staticContent
                    : null,
            ],
        ));
    }
}
