<?php

namespace Modules\Payroll\Repositories;

use Modules\Payroll\DTO\PayrollSettingFilter;
use Modules\Payroll\Enums\CpfAccountssStatusEnum;
use Modules\Payroll\Models\CpfAccounts;

class CpfAccountsRepository
{
    public function all(PayrollSettingFilter $filter, array $columns = ['*'], array $relationships = [])
    {
        return CpfAccounts::query()
            ->select($columns)
            ->with($relationships)
            ->search($filter->search)
            ->filterByStatus($filter->status)
            ->orderBy('opening_date', 'desc')
            ->paginate($filter->perPage ?? 15);
    }

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

    public function create(array $data)
    {
        return CpfAccounts::create([
            'employee_id'           => $data['employee_id'],
            'account_no'            => $data['account_no'] ?? null,
            'employee_percentage'   => $data['employee_percentage'],
            'employer_percentage'   => $data['employer_percentage'],
            'balance'               => $data['balance'] ?? 0,
            'opening_date'          => $data['opening_date'],
            'closing_date'          => $data['closing_date'] ?? null,
        ]);
    }

    public function update(CpfAccounts $cpfAccounts, array $data)
    {
        $cpfAccounts->update([
            'account_no'            => $data['account_no'] ?? null,
            'employee_percentage'   => $data['employee_percentage'],
            'employer_percentage'   => $data['employer_percentage'],
        ]);

        return $cpfAccounts->fresh();
    }
}
