<?php

namespace Modules\HR\Repositories;

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

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

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

    public function create(array $data)
    {
        return HolidayAllowancePolicy::create([
            'name' => $data['name'],
            'calculation_type' => $data['calculation_type'],
            'amount' => $data['amount'] ?? 0,
            'multiplier' => $data['multiplier'] ?? null,
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);
    }


    public function update(HolidayAllowancePolicy $holidayAllowancePolicy, array $data)
    {
        $holidayAllowancePolicy->update([
            'name' => $data['name'],
            'calculation_type' => $data['calculation_type'],
            'amount' => $data['amount'] ?? 0,
            'multiplier' => $data['multiplier'] ?? null,
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $holidayAllowancePolicy;
    }

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

        return $holidayAllowancePolicy;
    }
}
