<?php

namespace App\Modules\HR\Employees\Repositories;

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

class EmployeeAssetAssignmentRepository
{
    public function listForEmployee(int $tenantId, int $employeeId): Collection
    {
        return EmployeeAssetAssignment::query()
            ->where('tenant_id', $tenantId)
            ->where('employee_id', $employeeId)
            ->orderByDesc('assigned_at')
            ->orderBy('asset_name')
            ->get();
    }

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

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

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

        return $assignment->fresh();
    }

    public function delete(EmployeeAssetAssignment $assignment): void
    {
        $assignment->delete();
    }
}
