<?php


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


use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
use ThurData\Servers\KerioEmail\App\Traits\ExtensionsCheckerTrait;
use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
use ThurData\Servers\KerioEmail\Api\KerioWhmcs;

class ListAccounts extends AddonController
{
    use ExtensionsCheckerTrait;

    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()];
        }
        $api->logout();
        $accounts = [];       
        foreach($domains as $domain) {
            $accounts[] = [
                'domain' => $domain,
                'status' => 'Active',
                'primaryip' => $params['serverip'],
                'uniqueIdentifier' => $params['serverhostname'],
                'product' => '',
            ];

        }

        return [
            'success' => true,
            'accounts' => $accounts
        ];

    }
}