<?php

namespace Modules\Payroll\Services;

use Illuminate\Support\Facades\DB;
use Modules\Payroll\DTO\PayrollCyclesFilter;
use Modules\Payroll\Repositories\PayrollCycleRepository;
use Modules\Payroll\Models\PayrollCycle;

class PayrollCycleService
{

    public function __construct(protected PayrollCycleRepository $payrollCycleRepo) {}

    public function list(PayrollCyclesFilter $filter)
    {
        $columns = [
            'id',
            'name',
            'month',
            'year',
            'from_date',
            'to_date',
            'salary_date',
            'total_employees',
            'total_net_salary',
            'remarks',
            'is_locked',
            'status'
        ];

        return $this->payrollCycleRepo->all($filter, $columns);
    }

    public function show($id)
    {
        return $this->payrollCycleRepo->find($id);
    }

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

    public function update(PayrollCycle $payrollCycle, array $data)
    {
        return DB::transaction(function () use ($payrollCycle, $data) {
            return $this->payrollCycleRepo->update($payrollCycle, $data);
        });
    }

    public function updateStatus(PayrollCycle $payrollCycle, array $status)
    {
        return DB::transaction(function () use ($payrollCycle, $status) {
            return $this->payrollCycleRepo->updateStatus($payrollCycle, $status);
        });
    }
}
