<?php

namespace AC_SYNC\Includes\Classes\Async\Handlers;

if (!defined('ABSPATH')) exit;

use AC_SYNC\Includes\Classes\Async\Action_Wear_Job_Handler_Interface;
use AC_SYNC\Includes\Classes\Async\Action_Wear_Job_Result as JobResult;
use AC_SYNC\Includes\Classes\Logger\Action_Wear_Log as Log;
use AC_SYNC\Includes\Classes\Setting\Action_Wear_Setting_Debug_Mode as DebugMode;
use AC_SYNC\Includes\Classes\Product\Action_Wear_Product_Image as ProductImage;

class Action_Wear_Images_Update_Handler implements Action_Wear_Job_Handler_Interface
{
    public function handle(object $job, array $payload): JobResult
    {
        $sku = $payload['sku'] ?? null;
        if (!$sku) {
            return JobResult::fail('No SKU in payload');
        }

        $_product = new \WC_Product_Variable(wc_get_product_id_by_sku($sku));
        $productImage = new ProductImage($sku, $_product->get_id());
        $configurable_images = ProductImage::getImagesBySku($sku);
        $image_ids = $productImage->setConfigurableImages($configurable_images);
        $cover_id = false;

        if (DebugMode::getDebugMode()) {
            Log::write("Trovate le immagini del configurabile: " . implode(",", $configurable_images), Log::INFO, Log::CONTEXT_PRODUCT_CREATION);
        }

        if (count($image_ids) > 0) {
            $cover_id = $image_ids[0];
        }
        if ($cover_id !== false) {
            $_product->set_image_id($cover_id);
        }
        $_product->set_gallery_image_ids($image_ids);
        $_product->save();

        if (DebugMode::getDebugMode()) {
            Log::write("Acquisite e salvate immagini configurabile per sku $sku", Log::INFO, Log::CONTEXT_PRODUCT_CREATION);
        }

        $variations = $_product->get_available_variations();
        foreach ($variations as $variation) {
            $variation_id = $variation['variation_id'];
            $variation_obj = new \WC_Product_Variation($variation_id);
            $variation_sku = $variation_obj->get_sku();
            $cover = false;

            $simple_images = ProductImage::getImagesBySku($variation_sku);
            if (count($simple_images) > 0) {
                $image_ids = $productImage->setSimpleImagesBySku($variation_sku, $simple_images);
                if (DebugMode::getDebugMode()) {
                    Log::write("Impostate le immagini " . implode(",", $image_ids) . " del semplice con sku $variation_sku", Log::INFO, Log::CONTEXT_PRODUCT_CREATION);
                }
                if (count($image_ids) > 0) {
                    $cover = $image_ids[0];
                }
            }
            if ($cover !== false) {
                $variation_obj->set_image_id($cover);
            }
            $variation_obj->save();
        }
        $productImage->saveToMeta();

        return JobResult::success();
    }
}
