<?php

namespace Modules\HR\Repositories;

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

class HolidayGroupRepository
{
    public function all(HrSettingFilter $filter, array $columns = ['*'])
    {
        return HolidayGroup::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')
            ->select($columns)
            ->paginate($filter->perPage ?? 15);
    }

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

    public function create(array $data)
    {
        return HolidayGroup::create([
            'name'       => $data['name'],
            'description' => $data['description'] ?? null,
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);
    }

    public function update(HolidayGroup $holidayGroup, array $data)
    {
        $holidayGroup->update([
            'name'       => $data['name'],
            'description' => $data['description'] ?? null,
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $holidayGroup;
    }

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

        return $holidayGroup;
    }
}
