<?php
defined('ABSPATH') || exit;

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

        public function __construct() {
            add_action('init', array($this, 'pchfw_register_post_type'));
            add_action( 'add_meta_boxes', array( $this, 'pchfw_register_metabox' ) );
            add_action( 'save_post_pchfw', array( $this, 'pchfw_save_rules' ), 10, 2 );
            
        }

        public function pchfw_register_post_type() {
            $labels = array(
                'name'               => _x('Hide Rules', 'Post Type General Name', 'price-cart-hider-for-woocommerce'),
                'singular_name'      => _x('Hide Rule', 'Post Type Singular Name', 'price-cart-hider-for-woocommerce'),
                'menu_name'          => __('Price & Cart Rules', 'price-cart-hider-for-woocommerce'),
                'name_admin_bar'     => __('Hide Rule', 'price-cart-hider-for-woocommerce'),
                'add_new'            => __('Add New Rule', 'price-cart-hider-for-woocommerce'),
                'add_new_item'       => __('Add New Hide Rule', 'price-cart-hider-for-woocommerce'),
                'edit_item'          => __('Edit Hide Rule', 'price-cart-hider-for-woocommerce'),
                'new_item'           => __('New Hide Rule', 'price-cart-hider-for-woocommerce'),
                'view_item'          => __('View Rule', 'price-cart-hider-for-woocommerce'),
                'search_items'       => __('Search Rules', 'price-cart-hider-for-woocommerce'),
                'not_found'          => __('No rules found.', 'price-cart-hider-for-woocommerce'),
                'not_found_in_trash' => __('No rules found in Trash.', 'price-cart-hider-for-woocommerce'),
            );
        
            $args = array(
                'label'               => __('Hide Rules', 'price-cart-hider-for-woocommerce'),
                'description'         => __('Define rules to hide prices and Add to Cart button.', 'price-cart-hider-for-woocommerce'),
                'labels'              => $labels,
                'supports'            => array('title'),
                'public'              => false,
                'show_ui'             => true,
                'show_in_menu'        => 'woocommerce', // 👈 Show under WooCommerce menu
                'menu_position'       => 56,
                'capability_type'     => 'post',
                'has_archive'         => false,
                'rewrite'             => false,
            );
        
            register_post_type('pchfw', $args);
        }
        
        public function pchfw_register_metabox() {
            add_meta_box(
                'pchfw-meta-box',
                __( 'Hide Rule Settings', 'price-cart-hider-for-woocommerce' ),
                array( $this, 'pchfw_metabox_html' ),
                'pchfw',
                'advanced',
                'default'
            );
        }
		
		public function pchfw_metabox_html() {
			include_once PCHFW_PATH . 'includes/admin/views/metabox/html-pchfw-metabox.php';
		}

        public function pchfw_save_rules( $post_id, $post ) {
            // Check if this is an autosave
            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
                return;
            }

            // Check if this is a revision
            if ( wp_is_post_revision( $post_id ) ) {
                return;
            }

            // Check if this is the correct post type
            if ( 'pchfw' !== $post->post_type ) {
                return;
            }

            // Check user capabilities
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return;
            }

            // Verify metabox nonce
            if ( ! isset( $_POST['pchfw_metabox_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['pchfw_metabox_nonce'] ) ), 'pchfw_metabox_nonce' ) ) {
                return;
            }

            // Verify post nonce
            if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'update-post_' . $post_id ) ) {
                return;
            }

            // Sanitize and validate the rules data
            $rules_data = isset( $_REQUEST['pchfw_rule'] ) ? sanitize_meta('', wp_unslash($_REQUEST['pchfw_rule']), '') : array();

            update_post_meta( $post_id, 'pchfw_rule_data', $rules_data );
        }
		

    }

    new Price_Cart_Hider_Admin();
} 