<?php

namespace App\Services;

use App\Repositories\Interfaces\ConfigRepositoryInterface;

class ConfigService
{

    protected $configRepository;

    public function __construct(ConfigRepositoryInterface $configRepository)
    {
        $this->configRepository = $configRepository;
    }

    public function getConfigByType($type)
    {
        $configs = $this->configRepository->getByType($type);
        $fees = [];
        foreach ($configs as $config) {
            $fees[$config->key] = $config->value;
        }
        return $fees;
    }

    public function storeFees($request)
    {
        $payload = $request->all();
        unset($payload['_token']);

        foreach ($payload as $key => $value) {
            $fee = $this->configRepository->getByColumn('key', $key);

            if (empty($fee)) {
                $payload = [
                    'type' => 'fees',
                    'key' => $key,
                    'value' => $value
                ];
                $this->configRepository->create($payload);
            } else {
                $this->configRepository->update(['value' => $value], $fee->id);
            }
        }

        return;
    }
}
