<?php

namespace Modules\Payroll\Services;

use Illuminate\Support\Facades\DB;
use Modules\Core\Exceptions\ApiValidationException;
use Modules\Payroll\DTO\PayrollSettingFilter;
use Modules\Payroll\Enums\CpfAccountssEnum;
use Modules\Payroll\Repositories\CpfAccountsRepository;
use Modules\Payroll\Models\CpfAccounts;
use Modules\Payroll\Models\Traits\GenerateLoanNoTrait;

class CpfAccountsService
{
    use GenerateLoanNoTrait;

    public function __construct(protected CpfAccountsRepository $cpfAccountsRepo) {}

    public function list(PayrollSettingFilter $filter)
    {
        $columns = [
            'id',
            'employee_id',
            'account_no',
            'employee_percentage',
            'employer_percentage',
            'balance',
            'opening_date',
            'closing_date',
            'status'
        ];

        $relationships = [
            'employee:id,name,emp_number,phone',
        ];
        return $this->cpfAccountsRepo->all($filter, $columns, $relationships);
    }

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

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

    public function update(CpfAccounts $cpfAccounts, array $data)
    {
        return DB::transaction(function () use ($cpfAccounts, $data) {
            return $this->cpfAccountsRepo->update($cpfAccounts, $data);
        });
    }
}
