<?php

namespace Modules\HR\Repositories;

use Modules\Core\Enums\StatusEnum;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Models\TaxConfiguration;

class TaxConfigurationRepository
{
    public function all(HrSettingFilter $filter, array $columns = ['*'], $with = [])
    {
        return TaxConfiguration::query()
            ->when($filter->search, function ($query, $search) {
                $search = '%' . trim($search) . '%';

                $query->where(function ($q) use ($search) {
                    $q->whereHas('fiscalYear', function ($fiscalYearQuery) use ($search) {
                        $fiscalYearQuery->where('name', 'LIKE', $search);
                    });
                });
            })
            ->filterStatus($filter->status)
            ->select($columns)
            ->with($with)
            ->paginate($filter->perPage ?? 15);
    }

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

    public function create(array $data)
    {
        return TaxConfiguration::create([
            'fiscal_year_id'       => $data['fiscal_year_id'],
            'minimum_tax'         => $data['minimum_tax'],
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);
    }

    public function update(TaxConfiguration $taxConfiguration, array $data)
    {
        $taxConfiguration->update([
            'fiscal_year_id'       => $data['fiscal_year_id'],
            'minimum_tax'         => $data['minimum_tax'],
            'status'     => $data['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $taxConfiguration;
    }

    public function updateStatus(TaxConfiguration $taxConfiguration, array $status)
    {
        $taxConfiguration->update([
            'status' => $status['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $taxConfiguration;
    }
}
