<?php

namespace Modules\HR\Services;

use Illuminate\Support\Facades\DB;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Models\LeavePolicy;
use Modules\HR\Repositories\LeavePolicyRepository;

class LeavePolicyService
{
    protected $leavePolicyRepo;

    public function __construct(LeavePolicyRepository $leavePolicyRepo)
    {
        $this->leavePolicyRepo = $leavePolicyRepo;
    }

    public function list(HrSettingFilter $filter)
    {
        $columns = ['id', 'leave_type_id', 'annual_limit', 'carry_forward', 'encashable', 'max_carry_forward_days', 'status'];
        return $this->leavePolicyRepo->all($filter, $columns);
    }

    public function lookup(HrSettingFilter $filter)
    {
        $columns = ['id', 'leave_type_id', 'annual_limit', 'carry_forward', 'encashable', 'max_carry_forward_days', 'status'];
        return $this->leavePolicyRepo->all($filter, $columns);
    }

    public function show($id): ?LeavePolicy
    {
        return $this->leavePolicyRepo->find($id);
    }

    public function store(array $data)
    {
        return DB::transaction(function () use ($data) {
            return $this->leavePolicyRepo->create($data);
        });
    }

    public function update(LeavePolicy $leavePolicy, array $data)
    {
        return DB::transaction(function () use ($leavePolicy, $data) {
            return $this->leavePolicyRepo->update($leavePolicy, $data);
        });
    }

    public function updateStatus(LeavePolicy $leavePolicy, array $status)
    {
        return DB::transaction(function () use ($leavePolicy, $status) {
            return $this->leavePolicyRepo->updateStatus($leavePolicy, $status);
        });
    }
}
