<?php

use ModulesGarden\Servers\OpenXchange\App\Models\ProductConfiguration;
use function \ModulesGarden\Servers\OpenXchange\Core\Helper\di;

$hookManager->register(
    function ($vars)
    {
        $errors = [];
        $lang = di('lang');
        foreach($_SESSION['cart']['products'] as $product)
        {
            $productModel = \WHMCS\Product\Product::find($product['pid']);
            
            if($productModel->servertype != 'OpenXchange'){
                continue;
            }
            $config = ProductConfiguration::config($product['pid']);

            if($config['own_domains'] == "on"){
                $domain = \WHMCS\Domain\Domain::where('domain', $product['domain'])
                    ->whereIn('status', ['Active', 'Pending', 'Pending Transfer', 'Pending Registration'])
                    ->first();
                if(empty($domain->id)){
                    foreach($_SESSION['cart']['domains'] as $domain)
                    {
                        if ($domain['domain'] == $product['domain']) {
                            continue 2;
                        }
                    }
                    $errors[] = $lang->T('ownDomainNeed').$productModel->name.' ('.$product['domain'].')';
                }
            } else {
                continue;
            }
        }
        if(count($errors) > 0){
            return $errors;
        }
       
    }, 1
);
