<?php

namespace App\Modules\HR\Contracts\Repositories;

use App\Modules\HR\Contracts\Models\EmploymentContract;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

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

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

    public function paginateForTenant(int $tenantId, int $perPage = 20): LengthAwarePaginator
    {
        return EmploymentContract::query()
            ->where('tenant_id', $tenantId)
            ->with(['employee'])
            ->orderByDesc('start_date')
            ->paginate($perPage);
    }

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

        return $contract->fresh(['employee']);
    }
}
