<?php


namespace ModulesGarden\Servers\ZimbraEmail\App\Http\Actions;


use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
use ModulesGarden\Servers\ZimbraEmail\App\Traits\ExtensionsCheckerTrait;
use ModulesGarden\Servers\ZimbraEmail\Core\App\Controllers\Instances\AddonController;

class ListAccounts extends AddonController
{
    use ExtensionsCheckerTrait;

    public function execute($params = null)
    {
        try
        {
            $servers =(new ZimbraManager())
                ->getApiByServer($params['serverid'])
                ->soap
                ->repository()
                ->domains()
                ->getAll();

            $accounts = [];

            foreach ($servers as $server)
            {
                $created = explode('.', $server->getAttr('zimbraCreateTimestamp'));
                $created = date( 'Y-m-d H:i:s', strtotime($created[0]));

                $accounts[] = [
                    'domain' => $server->getName(),
                    'status' => 'Active',
                    'created' => $created,
                    'primaryip' => $params['serverip'],
                    'uniqueIdentifier' => $server->getName(),
                    'product' => '',
                ];
            }

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

        }
        catch (\Exception $ex)
        {
            return ['error' => $ex->getMessage()];
        }

    }
}