<?php

namespace App\Modules\HR\Offboarding\Repositories;

use App\Modules\HR\Employees\Models\EmployeeAssetAssignment;
use App\Modules\HR\Offboarding\Models\OffboardingCase;

class OffboardingRepository
{
    public function paginate(int $tenant)
    {
        return OffboardingCase::query()->with(['employee:id,full_name', 'checklistItems'])
            ->where('tenant_id', $tenant)->latest()->paginate();
    }

    public function create(array $data): OffboardingCase
    {
        return OffboardingCase::query()->create($data);
    }

    public function update(OffboardingCase $case, array $data): OffboardingCase
    {
        $case->update($data);

        return $case->fresh(['employee', 'checklistItems']);
    }

    public function returnAssets(int $tenant, int $employee): void
    {
        EmployeeAssetAssignment::query()->where('tenant_id', $tenant)->where('employee_id', $employee)
            ->whereNull('returned_at')->update(['status' => 'returned', 'returned_at' => now()->toDateString()]);
    }
}
