<?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\Product\Action_Wear_Product as Product;
use AC_SYNC\Includes\Classes\Logger\Action_Wear_Log as Log;

class Action_Wear_Catalog_Generate_Handler implements Action_Wear_Job_Handler_Interface
{
    /**
     * Map job_type suffix to Product static method.
     */
    private static $methodMap = [
        'catalog_generate_categories' => 'generateCategories',
        'catalog_generate_products'   => 'generateProducts',
        'catalog_generate_images'     => 'generateImages',
    ];

    public function handle(object $job, array $payload): JobResult
    {
        $method = self::$methodMap[$job->job_type] ?? null;

        if (!$method || !method_exists(Product::class, $method)) {
            return JobResult::fail("Unknown catalog generate method for job type: {$job->job_type}");
        }

        Product::$method();
        Log::write("CatalogGenerate: Executed Product::{$method}()", Log::INFO, Log::CONTEXT_CRONJOB);

        return JobResult::success();
    }
}
