<?php
if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Product_Admin
{
    public const FEATURED_META = '_puffergo_featured';

    public const CAT_ORDER_META = '_puffergo_cat_order';

    public const TOGGLE_NONCE_ACTION = 'puffergo_toggle_featured';

    public function __construct()
    {
        add_filter('manage_puffergo_product_posts_columns', array($this, 'add_featured_column'));
        add_action('manage_puffergo_product_posts_custom_column', array($this, 'render_featured_column'), 10, 2);
        add_action('admin_enqueue_scripts', array($this, 'enqueue_featured_column_assets'));
        add_action('wp_ajax_puffergo_toggle_featured', array($this, 'ajax_toggle_featured'));
        add_action('puffergo_product_cat_add_form_fields', array($this, 'render_cat_order_add_field'));
        add_action('puffergo_product_cat_edit_form_fields', array($this, 'render_cat_order_edit_field'));
        add_action('created_puffergo_product_cat', array($this, 'save_cat_order'));
        add_action('edited_puffergo_product_cat', array($this, 'save_cat_order'));
        add_filter('manage_edit-puffergo_product_cat_columns', array($this, 'add_cat_order_column'));
        add_filter('manage_puffergo_product_cat_custom_column', array($this, 'render_cat_order_column'), 10, 3);
    }

    public function add_featured_column($columns)
    {
        $columns['puffergo_featured'] = __('Featured', 'puffergo');

        return $columns;
    }

    public function render_featured_column($column, $post_id)
    {
        if ('puffergo_featured' !== $column) {
            return;
        } $is_featured = '1' === get_post_meta($post_id, self::FEATURED_META, true);
        $dashicon = $is_featured ? 'dashicons-star-filled' : 'dashicons-star-empty';
        $label = $is_featured ? __('Featured (pinned to the top) — click to remove', 'puffergo') : __('Click to feature — pins this product to the top of the list', 'puffergo'); ?>
		<button
			type="button"
			class="button-link puffergo-toggle-featured"
			data-post-id="<?php echo esc_attr($post_id); ?>"
			data-nonce="<?php echo esc_attr(wp_create_nonce(self::TOGGLE_NONCE_ACTION . '_' . $post_id)); ?>"
			aria-pressed="<?php echo $is_featured ? 'true' : 'false'; ?>"
			title="<?php echo esc_attr($label); ?>"
		>
			<span class="dashicons <?php echo esc_attr($dashicon); ?>" aria-hidden="true"></span>
			<span class="screen-reader-text"><?php echo esc_html($label); ?></span>
		</button>
		<?php
    }

    public function enqueue_featured_column_assets($hook)
    {
        global $post_type;

        if ('edit.php' !== $hook || 'puffergo_product' !== $post_type) {
            return;
        } $plugin_url = plugin_dir_url(PUFFERGO_PLUGIN_FILE);
        $plugin_dir = plugin_dir_path(PUFFERGO_PLUGIN_FILE);
        $asset_file = $plugin_dir . 'assets/js/puffergo-product-featured-column.js';
        wp_enqueue_script('puffergo-product-featured-column', $plugin_url . 'assets/js/puffergo-product-featured-column.js', array(), file_exists($asset_file) ? filemtime($asset_file) : false, true);
        wp_localize_script('puffergo-product-featured-column', 'puffergoFeaturedColumn', array('ajaxUrl' => admin_url('admin-ajax.php'), 'action' => 'puffergo_toggle_featured', ));
    }

    public function ajax_toggle_featured()
    {
        $post_id = isset($_POST['post_id']) ? absint(wp_unslash($_POST['post_id'])) : 0;

        if (! $post_id || 'puffergo_product' !== get_post_type($post_id)) {
            wp_send_json_error(array('message' => __('Invalid product.', 'puffergo')), 400);
        } check_ajax_referer(self::TOGGLE_NONCE_ACTION . '_' . $post_id, 'nonce');

        if (! current_user_can('edit_post', $post_id)) {
            wp_send_json_error(array('message' => __('You are not allowed to edit this product.', 'puffergo')), 403);
        } $is_featured = '1' === get_post_meta($post_id, self::FEATURED_META, true);

        if ($is_featured) {
            delete_post_meta($post_id, self::FEATURED_META);
        } else {
            update_post_meta($post_id, self::FEATURED_META, '1');
        } wp_send_json_success(array('featured' => ! $is_featured));
    }

    public function render_cat_order_add_field()
    { ?>
		<div class="form-field">
			<label for="puffergo_cat_order"><?php esc_html_e('Order', 'puffergo'); ?></label>
			<input type="number" step="1" name="puffergo_cat_order" id="puffergo_cat_order" value="0" />
			<p><?php esc_html_e('Used when the product category order is set to "Manual" in Product Settings. Lower numbers appear first.', 'puffergo'); ?></p>
		</div>
		<?php
    }

    public function render_cat_order_edit_field($term)
    {
        $value = (int) get_term_meta($term->term_id, self::CAT_ORDER_META, true); ?>
		<tr class="form-field">
			<th scope="row"><label for="puffergo_cat_order"><?php esc_html_e('Order', 'puffergo'); ?></label></th>
			<td>
				<input type="number" step="1" name="puffergo_cat_order" id="puffergo_cat_order" value="<?php echo esc_attr($value); ?>" />
				<p class="description"><?php esc_html_e('Used when the product category order is set to "Manual" in Product Settings. Lower numbers appear first.', 'puffergo'); ?></p>
			</td>
		</tr>
		<?php
    }

    public function save_cat_order($term_id)
    {
        if (! isset($_POST['puffergo_cat_order'])) {
            return;
        }

if (! current_user_can('manage_categories')) {
            return;
        } check_admin_referer('update-tag_' . $term_id);
        $order = absint(wp_unslash($_POST['puffergo_cat_order']));
        update_term_meta($term_id, self::CAT_ORDER_META, $order);
    }

    public function add_cat_order_column($columns)
    {
        $columns['puffergo_cat_order'] = __('Order', 'puffergo');

        return $columns;
    }

    public function render_cat_order_column($content, $column_name, $term_id)
    {
        if ('puffergo_cat_order' !== $column_name) {
            return $content;
        }

return (string) (int) get_term_meta($term_id, self::CAT_ORDER_META, true);
    }
}
