<?php

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


use ThurData\Servers\KerioEmail\App\Enums\Response;
use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
use ThurData\Servers\KerioEmail\App\Traits\ExtensionsCheckerTrait;
use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
use ThurData\Servers\KerioEmail\Api\KerioWhmcs;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 09.09.19
 * Time: 15:14
 * Class TerminateAccount
 */
class TerminateAccount extends AddonController
{
    use ExtensionsCheckerTrait;

    public function execute($params = null)
    {
        try{

            /**
             * check if extensions are installed
             */
            $this->checkExtensionOrThrowError();

            /**
             * run kerio service
             */
            $result =  $this->kerioRunService($params);
            return $result;
        }catch (\Exception $ex)
        {
            /**
             * return some crit error
             */
            return $ex->getMessage();
        }

    }

    /**
     * @param null $params
     * @return mixed|string
     */
    protected function kerioRunService($params = null)
    {
        $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
        try {
            $api->login($params['serverhostname'], $params['serverusername'], $params['serverpassword']);
            $domainID = $api->getDomainId($params['domain']);
        } catch (KerioApiException $error) {
            logModuleCall(
                'kerioEmail',
                __FUNCTION__,
                $error,
                'Debug Error',
                $error->getMessage()
            );
            return ['error' => $error->getMessage()];
        }             
        if ($domainID === FALSE) {
            return "Error: Domain $domain not found";
        }
        // remove all users from domain
        try {
            $users = $api->getUsers(['id', 'loginName'], $domainID);
        } catch (KerioApiException $error) {
            logModuleCall(
                'kerioEmail',
                __FUNCTION__,
                $error,
                'Debug Error',
                $error->getMessage()
            );
            return ['error' => $error->getMessage()];
        }             

        foreach($users as $user) {
            try {
                $api->deleteUser($user['id']);
            } catch (KerioApiException $error) {
                logModuleCall(
                    'kerioEmail',
                    __FUNCTION__,
                    $error,
                    'Debug Error',
                    $error->getMessage()
                );
                return ['error' => $error->getMessage()];
            }             
        }

        // finally remove domain
        try {
            $result = $api->deleteDomain($domainID);
        } catch (KerioApiException $error) {
            logModuleCall(
                'kerioEmail',
                __FUNCTION__,
                $error,
                'Debug Error',
                $error->getMessage()
            );
            return ['error' => $error->getMessage()];
        }             
        $api->logout();

        /**
         * return success response
         */
        return Response::SUCCESS;
    }
}