<?php

namespace App\Modules\HR\Recruitment\Repositories;

use App\Modules\HR\Recruitment\Models\RecruitmentCareerPortalSetting;

class CareerPortalSettingsRepository
{
    public function findForTenant(int $tenantId): ?RecruitmentCareerPortalSetting
    {
        return RecruitmentCareerPortalSetting::query()
            ->withoutGlobalScopes()
            ->where('tenant_id', $tenantId)
            ->first();
    }

    /** @param  array<string, mixed>  $data */
    public function upsert(int $tenantId, array $data): RecruitmentCareerPortalSetting
    {
        $row = $this->findForTenant($tenantId);
        if ($row === null) {
            return RecruitmentCareerPortalSetting::query()->create(array_merge($data, [
                'tenant_id' => $tenantId,
            ]));
        }
        $row->update($data);

        return $row->fresh();
    }
}
