<?php

namespace ModulesGarden\Servers\ZimbraEmail\Core\Helper;

use ModulesGarden\Servers\ZimbraEmail\Core\FileReader\Reader;
use ModulesGarden\Servers\ZimbraEmail\Core\ServiceLocator;
use WHMCS\Database\Capsule;

/**
 * Autometes some of database queries
 *
 * @author
 */
class ConfigOptionsHelper
{
    
    /**
     * Helper to perform Configurable Options queries for module
     *
     * @param int $productID
     * @param string $optionName
     * @return Object
     */
    public function getConfigurableOption(int $productID, string $optionName) {
        $configOption = Capsule::table('tblproductconfigoptions')
            ->join('tblhostingconfigoptions', 'tblproductconfigoptions.id', '=', 'tblhostingconfigoptions.configid')
            ->where('tblhostingconfigoptions.relid', '=', $productID)
            ->where('tblproductconfigoptions.optionname', 'like', $optionName.'%')
            ->select('*')
            ->get();
        return $configOption->pull(0);
    }
    /**
     * Helper to perform Configurable Options queries for module
     *
     * @param int $optionID
     * @param array $optionValues
     * @return bool
     */
    public function updateConfigurableOption(int $optionID, array $optionValues) {
        try {
            $updateOption = Capsule::table('tblhostingconfigoptions')
                ->where('id', $optionID)
                ->update($optionValues);
        } catch (\Exception $e) {
            logModuleCall(
                'zimbraEmail',
                __FUNCTION__,
                $updateOption,
                'Error: could not update option ' . $optionID .' in database.',
                $e->getMessage()
            );
            return false;
        }
        return true;
    }
}
