<?php
namespace Modules\Employee\Repositories;

use Modules\Core\Enums\StatusEnum;
use Modules\Employee\DTO\EmployeeSettingFilter;
use Modules\Employee\Models\StaffType;


class StaffTypeRepository
{
    public function all(EmployeeSettingFilter $filter)
    {
        return StaffType::when($filter->search, function ($query, $search) {
                $search = '%' . trim($search) . '%';

                $query->where(function ($q) use ($search) {
                    $q->where('name', 'LIKE', $search);
                });
            })
            ->when($filter->status, fn ($q, $status) =>
                $q->where('status', $status)
            )
            ->orderBy('name')
            ->paginate($filter->perPage ?? 15);
    }

    public function find($id)
    {
        $staffType = StaffType::findOrFail($id);
        return $staffType;
    }

    public function create(array $data)
    {
        $staffType = StaffType::create([
            'name'       => $data['name'],
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $staffType;
    }

    public function update(StaffType $staffType, array $data)
    {
        $staffType->update([
            'name'       => $data['name'],
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $staffType;
    }

    public function updateStatus(StaffType $staffType, array $status)
    {
        $staffType->update([
            'status' => $status['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $staffType;
    }
}
