<?php
namespace Modules\Employee\Repositories;

use Illuminate\Support\Facades\DB;
use Modules\Core\Enums\StatusEnum;
use Modules\Employee\DTO\EmployeeSettingFilter;
use Modules\Employee\Models\Department;


class DepartmentRepository
{
    public function all(EmployeeSettingFilter $filter)
    {
        return Department::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)
    {
        $department = Department::findOrFail($id);
        return $department;
    }

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

        return $department;
    }

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

        return $department;
    }

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

        return $department;
    }
}
