<?php

namespace Licenzo\Modules\WooCommerce;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class WooOrderHandler
{
    public function handle(): void
    {
        add_action('woocommerce_order_status_on-hold', [$this, 'process'], 10, 1);
        add_action('woocommerce_order_status_processing', [$this, 'process'], 10, 1);
        add_action('woocommerce_order_status_completed', [$this, 'process'], 10, 1);
        add_action('woocommerce_order_status_changed', [$this, 'syncStatus'], 10, 3);
    }

    public function process($orderId): void
    {
        if (!$orderId) {
            return;
        }
        (new WooOrderService())->process((int) $orderId);
    }

    public function syncStatus($orderId, $fromStatus, $toStatus): void
    {
        if (!$orderId) {
            return;
        }

        (new WooOrderService())->syncOrderStatus((int) $orderId, (string) $fromStatus, (string) $toStatus);
    }
}
