<?php

namespace ThurData\Servers\KerioEmail\Core\Helper;

use ThurData\Servers\KerioEmail\Core\FileReader\Reader;
use ThurData\Servers\KerioEmail\Core\ServiceLocator;
use WHMCS\Database\Capsule;

/**
 * Autometes some of database queries
 *
 * @author
 */
class UpdateLimit
{
    
    /**
     * Helper to perform DiskLimit queries for module
     *
     * @param int $serviceID
     * @return Object
     */
    public function getDiskLimit(int $serviceID) {
        $diskLimit = Capsule::table('tblhosting')
            ->where('id', '=', $serviceID)
            ->select('disklimit')
            ->get();
        return $diskLimit->pull(0);
    }
    /**
     * Helper to perform DiskLimit queries for module
     *
     * @param int $serviceID
     * @return Object
     */
    public function getBWLimit(int $serviceID) {
        $BWLimit = Capsule::table('tblhosting')
            ->where('id', '=', $serviceID)
            ->select('bwlimit')
            ->get();
        return $BWLimit->pull(0);
    }
    /**
     * Helper to perform DiskLimit queries for module
     *
     * @param int $serviceID
     * @param int $diskLimitValue
     * @return bool
     */
    public function updateDiskLimit(int $serviceID, int $diskLimitValue) {
        try {
            $diskLimit = Capsule::table('tblhosting')
                ->where('id', $serviceID)
                ->update(['disklimit' => $diskLimitValue]);
        } catch (\Exception $e) {
            logModuleCall(
                'kerioEmail',
                __FUNCTION__,
                $diskLimit,
                'Error: could not update disklimit for ' . $serviceID .' in database.',
                $e->getMessage()
            );
            return false;
        }
        return true;
    }
    /**
     * Helper to perform BWLimit queries for module
     *
     * @param int $serviceID
     * @param int $usageUpdateValue
     * @return bool
     */
    public function updateBWLimit(int $serviceID, int $bwLimitValue) {

        try {
            $usage = Capsule::table('tblhosting')
                ->where('id', $serviceID)
                ->update(['bwlimit' => $bwLimitValue]);
        } catch (\Exception $e) {
            logModuleCall(
                'kerioEmail',
                __FUNCTION__,
                $usage,
                'Error: could not update bwlimit for ' . $serviceID .' in database.',
                $e->getMessage()
            );
            return false;
        }
        return true;
    }
}
