<?php

namespace Modules\HR\Services;

use Illuminate\Support\Facades\DB;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Models\SalaryComponent;
use Modules\HR\Models\Traits\GenerateSalaryComponentCodeTraits;
use Modules\HR\Repositories\SalaryComponentRepository;

class SalaryComponentService
{
    use GenerateSalaryComponentCodeTraits;
    protected $salaryComponentRepo;

    public function __construct(SalaryComponentRepository $salaryComponentRepo)
    {
        $this->salaryComponentRepo = $salaryComponentRepo;
    }

    public function list(HrSettingFilter $filter)
    {
        $columns = ['id', 'name', 'code', 'type',  'status'];
        return $this->salaryComponentRepo->all($filter, $columns);
    }

    public function lookup(HrSettingFilter $filter)
    {
        $columns = ['id', 'name', 'code', 'type','is_basic', 'status'];
        return $this->salaryComponentRepo->all($filter, $columns);
    }

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

    public function store(array $data)
    {
        return DB::transaction(function () use ($data) {
            if (blank($data['code'] ?? null)) {
                $data['code'] = $this->generateSalaryComponentCode();
            }
            return $this->salaryComponentRepo->create($data);
        });
    }

    public function update(SalaryComponent $salaryComponent, array $data)
    {
        return DB::transaction(function () use ($salaryComponent, $data) {
            return $this->salaryComponentRepo->update($salaryComponent, $data);
        });
    }

    public function updateStatus(SalaryComponent $salaryComponent, array $status)
    {
        return DB::transaction(function () use ($salaryComponent, $status) {
            return $this->salaryComponentRepo->updateStatus($salaryComponent, $status);
        });
    }
}
