<?php

namespace App\Modules\HR\Recruitment\Repositories;

use App\Modules\HR\Recruitment\Models\RecruitmentReportSavedFilter;
use Illuminate\Database\Eloquent\Collection;

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

    public function deleteOwned(int $tenantId, int $userId, int $id): void
    {
        RecruitmentReportSavedFilter::query()
            ->where('tenant_id', $tenantId)
            ->where('user_id', $userId)
            ->whereKey($id)
            ->delete();
    }

    public function findOwned(int $tenantId, int $userId, int $id): ?RecruitmentReportSavedFilter
    {
        return RecruitmentReportSavedFilter::query()
            ->where('tenant_id', $tenantId)
            ->where('user_id', $userId)
            ->whereKey($id)
            ->first();
    }

    /** @return Collection<int, RecruitmentReportSavedFilter> */
    public function forUserReport(int $tenantId, int $userId, string $reportType): Collection
    {
        return RecruitmentReportSavedFilter::query()
            ->where('tenant_id', $tenantId)
            ->where('user_id', $userId)
            ->where('report_type', $reportType)
            ->orderByDesc('is_default')
            ->orderBy('name')
            ->get();
    }

    public function clearDefaults(int $tenantId, int $userId, string $reportType): void
    {
        RecruitmentReportSavedFilter::query()
            ->where('tenant_id', $tenantId)
            ->where('user_id', $userId)
            ->where('report_type', $reportType)
            ->update(['is_default' => false]);
    }
}
