<?php

namespace Modules\HR\Repositories;

use Modules\Core\Enums\StatusEnum;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Models\LeaveType;

class LeaveTypeRepository
{
    public function all(HrSettingFilter $filter, $columns = ['*'])
    {
        return LeaveType::query()
            ->search($filter->search)
            ->filterStatus($filter->status)
            ->orderBy('name', 'asc')
            ->select($columns)
            ->paginate($filter->perPage ?? 15);
    }

    public function find($id)
    {
        return LeaveType::findOrFail($id);
    }

    public function create(array $data)
    {
        return LeaveType::create([
            'name' => $data['name'],
            'days_per_year' => $data['days_per_year'],
            'is_paid' => $data['is_paid'] ?? false,
            'is_encashable' => $data['is_encashable'],
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);
    }


    public function update(LeaveType $leaveType, array $data)
    {
        $leaveType->update([
            'name' => $data['name'],
            'days_per_year' => $data['days_per_year'],
            'is_paid' => $data['is_paid'],
            'is_encashable' => $data['is_encashable'],
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $leaveType;
    }

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

        return $leaveType;
    }
}
