<?php
namespace Bright_Deposits_For_WooCommerce\Core;

use Bright_Deposits_For_WooCommerce\Helpers\Helper;

/**
 * @package Bright_Deposits_For_WooCommerce\Core
 * @since 1.0.0
 * @author Bright Plugins
 * Description: This class is responsible for managing the exclusion of products from deposits.
 */
class ExclusionManager {

    /**
	 * Singleton instance of the class.
	 */
	private static $instance = null;

    /**
	 * Get the singleton instance of the class.
	 * @since 1.0.0
	 * @return Checkout
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Prevents cloning of the instance.
	 * @since 1.0.0
	 * @return void
	 */
	private function __clone() {
		// Cloning is forbidden.
	}

	/**
	 * Prevents unserializing of the instance.
	 * @since 1.0.0
	 * @return void
	 */
	public function __wakeup() {
		// Unserializing instances of this class is forbidden.
	}

    /**
     * Check if a product is excluded from deposits based on plugin settings.
     * @since 1.0.0
     * @param \WC_Product|int $product The product object or product ID.
     * @return bool True if the product is excluded, false otherwise.
     */
    public function is_product_excluded( $product ): bool {
        if ( ! $product instanceof \WC_Product ) {
            $product = wc_get_product( $product );
        }

        if ( ! $product ) {
            return false;
        }

        //Check if the product is individually excluded
        $is_excluded = $this->is_excluded_by_product( $product );
        
        if ( $is_excluded ) {
            return true;
        }

        return false;
    }

    /**
     * Check if the product is individually excluded.
     * @since 1.0.0
     * @param WC_Product $product The product object.
     * @return bool True if excluded, false otherwise.
     */
    private function is_excluded_by_product( \WC_Product $product ) {
        // Get the excluded products and if empty, return false
        $excluded_products = Helper::getOptionValue( 'excluded_products' );
        
        // Return true if the product is in the excluded products
        return ! empty( $excluded_products ) && in_array( $product->get_id(), $excluded_products );
    }
    
}