<?php


namespace ThurData\Servers\KerioEmail\App\Http\Actions;


use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
use ThurData\Servers\KerioEmail\Core\Models\Whmcs;
use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
use WHMCS\Database\Capsule;

class UsageUpdate extends AddonController
{
    public function execute($params = null)
    {
        $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
        try {
            $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
            $domains = $api->getDomains(['id','name']);
        } catch (KerioApiException $error) {
            logModuleCall(
                'kerioEmail',
                __FUNCTION__,
                $error,
                'Debug Error',
                $error->getMessage()
            );
            return ['error' => $error->getMessage()];
        }

        foreach ($domains as $domain)
        {
            try
            {
                $domainName = $domain['name'];
                $domainID = $domain['id'];
                $fields = array(
					'loginName',
					'fullName',
					'consumedSize'
                );
                $users = $api->getUsers($fields,$domainID);
                $mailboxes = count($users);
                $totalUsage = 0;
                if ($mailboxes > 0) {
                    foreach ($users as $user) {
                        switch($user['consumedSize']['units']) {
                            case 'Bytes' :
                                $usage = $user['consumedSize']['value']/1024/1024;
                                break;
                            case 'KiloBytes' :
                                $usage = $user['consumedSize']['value']/1024;
                                break;
                            case 'MegaBytes' :
                                $usage = $user['consumedSize']['value'];
                                break;
                            case 'GigaBytes' :
                                $usage = $user['consumedSize']['value']*1024;
                                break;
                        }
                        $totalUsage += $usage;
                    }
                }

                $storage = $totalUsage == 0 ? 0 : round($totalUsage);
                $serverId = $params['serverid'];

                Whmcs\Hosting::where('server', $serverId)->where('domain', $domainName)->update(['bwusage' => $mailboxes]);
                Whmcs\Hosting::where('server', $serverId)->where('domain', $domainName)->update(['diskusage' => $storage]);
                Whmcs\Hosting::where('server', $serverId)->where('domain', $domainName)->update(['lastupdate' => Capsule::raw('now()')]);
                usleep(5000);
            } catch (KerioApiException $error) {
                logModuleCall(
                    'kerioEmail',
                    __FUNCTION__,
                    $error,
                    'Debug Error',
                    $error->getMessage()
                );
                return ['error' => $error->getMessage()];
            }
        }
    }
}