<?php

namespace App\Modules\HR\Recruitment\Repositories;

use App\Modules\HR\Recruitment\Models\RecruitmentJobRequest;
use App\Support\SelectOptionFormatter;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

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

    /**
     * @param  array<string, mixed>  $data
     */
    public function update(RecruitmentJobRequest $row, array $data): RecruitmentJobRequest
    {
        $row->update($data);

        return $row->fresh(['department', 'position']);
    }

    public function softDelete(RecruitmentJobRequest $row): void
    {
        $row->delete();
    }

    public function lockForTenant(int $tenantId, int $id): ?RecruitmentJobRequest
    {
        return RecruitmentJobRequest::query()
            ->where('tenant_id', $tenantId)
            ->whereKey($id)
            ->lockForUpdate()
            ->first();
    }

    /**
     * @param  array{q?: string|null, status?: string|null, department_id?: int|null}  $filters
     */
    public function paginateForTenant(int $tenantId, array $filters = [], int $perPage = 20): LengthAwarePaginator
    {
        $q = RecruitmentJobRequest::query()
            ->where('tenant_id', $tenantId)
            ->with(['department', 'position'])
            ->orderByDesc('id');

        if (! empty($filters['status'])) {
            $q->where('status', $filters['status']);
        }
        if (! empty($filters['department_id'])) {
            $q->where('department_id', (int) $filters['department_id']);
        }
        if (! empty($filters['q'])) {
            $term = '%'.$filters['q'].'%';
            $q->where(function ($builder) use ($term): void {
                $builder->where('title', 'like', $term)
                    ->orWhere('reference_code', 'like', $term);
            });
        }

        return $q->paginate($perPage)->withQueryString();
    }

    /**
     * @return list<array{value: string, label: string}>
     */
    public function searchForLookup(int $tenantId, string $query, int $limit = 30, ?int $onlyId = null, ?string $status = null): array
    {
        $builder = RecruitmentJobRequest::query()
            ->where('tenant_id', $tenantId)
            ->orderByDesc('id');

        if ($status !== null) {
            $builder->where('status', $status);
        }
        if ($onlyId !== null) {
            $builder->whereKey($onlyId);
        } else {
            if ($query !== '') {
                $term = '%'.$query.'%';
                $builder->where(function ($q) use ($term): void {
                    $q->where('title', 'like', $term)->orWhere('reference_code', 'like', $term);
                });
            }
            $builder->limit($limit);
        }

        return SelectOptionFormatter::mapRows(
            $builder->get(['id', 'title', 'reference_code']),
            'title',
            'reference_code',
        );
    }
}
