<?php
include 'connect.php';
$business = json_decode(file_get_contents('php://input'));
$business_id = $business->data->id;

if(!isset($business->params->mode)){
    foreach($business->data as $bus){

        if(sizeof($bus->offers) > 0){
        $businessidd = [$bus->id];
        $businessidd = json_encode($businessidd);

        $url1 = $site_api_url_fetch.'/orders?orderBy=-id&page=1&page_size=50&where={"conditions":[{"conector":"OR","conditions":[{"attribute":"products","conditions":[{"attribute":"type","value":{"condition":"=","value":"item"}}]},{"attribute":"cloned_order_id","value":{"condition":"!=","value":null}},{"attribute":"delivery_type","value":{"condition":"=","value":9}}]},{"attribute":"status","value":[0,13,1,11,15,7,8,4,9,3,14,18,19,20,21,22,23,24,25,26]},{"conector":"AND","conditions":[{"attribute":"business_id","value":'.$businessidd.'}]}],"conector":"AND"}&mode=dashboard';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Accept: application/json',
        'x-api-key: '.$user_key
        ));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $busordresult = curl_exec($ch);
        $busorders = json_decode($busordresult);
        curl_close($ch);


        $newArray = [];

        foreach($busorders->result as $ordbus){

            foreach ($ordbus->products as $product) {
                foreach ($product->options as $option) {

                    // Collect suboptions for comparison
                    $suboptionsArray = [];
                    foreach ($option->suboptions as $subop) {
                        $suboptionsArray[] = [
                            'suboption_id' => $subop->id,
                            'suboption_name' => $subop->name,
                        ];
                    }

                    // Check if same product + option + suboptions already exist
                    $exists = false;
                    foreach ($newArray as $existing) {
                        foreach ($existing->options as $existingOption) {
                            $existingSuboptionsArray = [];
                            foreach ($existingOption->suboptions as $exSub) {
                                $existingSuboptionsArray[] = [
                                    'suboption_id' => $exSub->id,
                                    'suboption_name' => $exSub->name,
                                ];
                            }

                            if (
                                $existing->product_id == $product->product_id &&
                                $existingOption->id == $option->id &&
                                $existingSuboptionsArray == $suboptionsArray
                            ) {
                                $exists = true;
                                break 2; // stop both loops
                            }
                        }
                    }

                    if (!$exists) {
                        // Push the full product object
                        $newArray[] = $product;
                        break; // stop checking other options of this product
                    }
                }
            }
        }
        $bus->preProductsdata = $newArray;
        }
    }
}
echo json_encode($business->data);
?>
