<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class Dspilot_Plugin {

    private $plugin_options = array();


    public function __construct() {
        $this->plugin_options = get_option( 'dspilot_options', array() );
        // Codestar settings save hook
        add_action( 'dspilot_dspilot_options_save_after', [$this, 'clear_wc_transients_after_csf_save'] );
    }


    /**
     * Get plugin options
     *
     * @return array
     */
    public static function get_plugin_options() {
        return get_option( 'dspilot_options', array() );
    }


    /**
     * Get active discounts from plugin options
     *
     * @return array
     */
    public static function get_discounts($discount_type = 'pricing_discounts') {
        $options = self::get_plugin_options();
        $disocunts = isset( $options[$discount_type] ) ? $options[$discount_type] : array();
        $active_discounts = [];
        if ( ! empty( $disocunts ) && is_array( $disocunts ) ) {
            foreach ( $disocunts as $discount ) {
                if ( isset( $discount['status'] ) && $discount['status'] == 1 ) {
                    $active_discounts[] = array_merge(
                        $discount,
                        [
                            'id'   => sanitize_title( $discount['id'] )
                        ]
                    );
                }
            }

        }
        return $active_discounts;
    }

    /**
     * Clear WooCommerce transients after CSF settings save
     *
     * @param array $options The options array saved by CSF.
     */
    function clear_wc_transients_after_csf_save( $options ) {
        // Clear all product transients
        wc_delete_product_transients( 0 );

        // Optional: clear WooCommerce cache objects
        if ( function_exists( 'wc_delete_shop_order_transients' ) ) {
            wc_delete_shop_order_transients();
        }

        // Optional: clear WooCommerce session/cart transients
        if ( function_exists( 'wc_delete_cart_transients' ) ) {
            wc_delete_cart_transients();
        }

        // Optional: clear other transients related to prices, widgets, etc.
        // global $wpdb;
        // $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_wc_%'" );
    }

}

new Dspilot_Plugin();