<?php

namespace App\Modules\HR\Employees\Repositories;

use App\Modules\HR\Employees\Models\EmployeeEmploymentHistory;
use Illuminate\Support\Collection;

class EmployeeEmploymentHistoryRepository
{
    public function listForEmployee(int $tenantId, int $employeeId): Collection
    {
        return EmployeeEmploymentHistory::query()->where('tenant_id', $tenantId)
            ->where('employee_id', $employeeId)->with('createdBy:id,name')
            ->orderByDesc('effective_date')->orderByDesc('id')->get();
    }

    public function findForEmployee(int $tenantId, int $employeeId, int $id): ?EmployeeEmploymentHistory
    {
        return EmployeeEmploymentHistory::query()->where('tenant_id', $tenantId)
            ->where('employee_id', $employeeId)->whereKey($id)->first();
    }

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

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

        return $history->fresh();
    }

    public function delete(EmployeeEmploymentHistory $history): void
    {
        $history->delete();
    }
}
