<?php

namespace App\Modules\HR\Recruitment\Repositories;

use App\Modules\HR\Recruitment\Models\RecruitmentOnboardingAttachment;
use App\Modules\HR\Recruitment\Models\RecruitmentOnboardingTask;
use Illuminate\Support\Collection;

class RecruitmentOnboardingTaskRepository
{
    /** @param  array<string, mixed>  $data */
    public function create(array $data): RecruitmentOnboardingTask
    {
        return RecruitmentOnboardingTask::query()->create($data);
    }

    public function findForTenant(int $tenantId, int $id): ?RecruitmentOnboardingTask
    {
        return RecruitmentOnboardingTask::query()
            ->where('tenant_id', $tenantId)
            ->whereKey($id)
            ->with(['onboardingCase', 'attachments'])
            ->first();
    }

    /** @return Collection<int, RecruitmentOnboardingTask> */
    public function requiredForCase(int $caseId): Collection
    {
        return RecruitmentOnboardingTask::query()
            ->where('onboarding_case_id', $caseId)
            ->where('is_required', true)
            ->get();
    }

    /** @param  array<string, mixed>  $data */
    public function update(RecruitmentOnboardingTask $task, array $data): RecruitmentOnboardingTask
    {
        $task->fill($data)->save();

        return $task->refresh();
    }

    /** @param  array<string, mixed>  $data */
    public function createAttachment(array $data): RecruitmentOnboardingAttachment
    {
        return RecruitmentOnboardingAttachment::query()->create($data);
    }

    public function findAttachment(int $tenantId, int $id): ?RecruitmentOnboardingAttachment
    {
        return RecruitmentOnboardingAttachment::query()
            ->where('tenant_id', $tenantId)
            ->whereKey($id)
            ->with('task.onboardingCase')
            ->first();
    }
}
