<?php

namespace AC_SYNC\Includes\Classes\Setting {

    if (!defined('ABSPATH'))
        exit; // Exit if accessed directly

    /**
     * Setting for order sync trigger status
     * Determines which WooCommerce order status triggers automatic sync to ActionWear
     *
     * Possible values: 'disabled', 'processing', 'completed', 'on-hold'
     */
    class Action_Wear_Setting_Order_Sync_Trigger implements Action_Wear_Setting_Entity
    {
        const OPTION_NAME = '_ACTIONWEAR_ORDER_SYNC_TRIGGER';
        const DEFAULT_VALUE = 'disabled';

        const VALID_VALUES = ['disabled', 'processing', 'completed', 'on-hold'];

        public function toggle()
        {
            return null;
        }

        public function get()
        {
            return get_option(self::OPTION_NAME, self::DEFAULT_VALUE);
        }

        public function validate($v): bool
        {
            return is_string($v) && in_array($v, self::VALID_VALUES);
        }

        public function set($value)
        {
            if (!$this->validate($value)) {
                throw new \Exception("Invalid value for order sync trigger. Must be one of: " . implode(', ', self::VALID_VALUES));
            }
            update_option(self::OPTION_NAME, (string) $value);
            return $this->get();
        }

        /**
         * Static helper to get the current trigger status
         *
         * @return string Current trigger status
         */
        public static function getTriggerStatus()
        {
            return get_option(self::OPTION_NAME, self::DEFAULT_VALUE);
        }

        /**
         * Check if order sync is enabled
         *
         * @return bool
         */
        public static function isEnabled()
        {
            return self::getTriggerStatus() !== 'disabled';
        }
    }
}
