<?php
/**
 * Admin settings and configuration
 */

if (!defined('ABSPATH')) {
    exit;
}

if (!class_exists('WC_TPP_Admin')) {
    class WC_TPP_Admin {

    private static $instance = null;
    private static $settings_instance = null;

    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_filter('woocommerce_get_settings_pages', array($this, 'add_settings_page'));
        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
    }

    /**
     * Add settings page to WooCommerce settings
     */
    public function add_settings_page($settings) {
        if (null === self::$settings_instance) {
            require_once WC_TPP_PLUGIN_DIR . 'includes/class-wc-tpp-settings.php';
            self::$settings_instance = new WC_TPP_Settings();
        }

        // Check if our settings page is already in the array to prevent duplicates
        // Check by class type and ID instead of instance comparison
        foreach ($settings as $settings_page) {
            if ($settings_page instanceof WC_TPP_Settings) {
                return $settings;
            }
            // Also check by ID property if it's a WC_Settings_Page
            if (is_object($settings_page) &&
                method_exists($settings_page, 'get_id') &&
                $settings_page->get_id() === 'tier_package_prices') {
                return $settings;
            }
            // Check id property directly
            if (is_object($settings_page) &&
                isset($settings_page->id) &&
                $settings_page->id === 'tier_package_prices') {
                return $settings;
            }
        }

        $settings[] = self::$settings_instance;
        return $settings;
    }

    public function enqueue_admin_scripts($hook) {
        if ('woocommerce_page_wc-settings' === $hook || 'post.php' === $hook || 'post-new.php' === $hook) {
            wp_enqueue_style('wc-tpp-admin', WC_TPP_PLUGIN_URL . 'assets/css/admin.css', array(), WC_TPP_VERSION);
            wp_enqueue_script('wc-tpp-admin', WC_TPP_PLUGIN_URL . 'assets/js/admin.js', array('jquery'), WC_TPP_VERSION, true);
        }
    }
}

WC_TPP_Admin::get_instance();
}
