<?php
/**
 * Product Admin Class
 *
 * Handles product-level fields like Cost of Goods.
 *
 * @package InsightPress
 */

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

class InsightPress_Product_Admin
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        // Add Cost of Goods field to Product General tab
        add_action('woocommerce_product_options_general_product_data', array($this, 'add_cogs_field'));

        // Save Cost of Goods field
        add_action('woocommerce_process_product_meta', array($this, 'save_cogs_field'));
    }

    /**
     * Add Cost of Goods field to the General tab.
     */
    public function add_cogs_field()
    {
        global $post;

        echo '<div class="options_group">';

        $is_pro = insightpress_is_pro();
        $label = sprintf(
            /* translators: %s: Currency symbol */
            __('Cost of Goods (%s)', 'insightpress-advanced-analytics-for-woocommerce'),
            get_woocommerce_currency_symbol()
        );

        if (!$is_pro) {
            $label .= ' <span style="background:#7c3aed;color:#fff;padding:2px 6px;border-radius:4px;font-size:10px;text-transform:uppercase;margin-left:5px;">PRO</span>';
        }

        // Cost of Goods Field
        woocommerce_wp_text_input(
            array(
                'id' => '_insightpress_cogs',
                'label' => $label,
                'description' => $is_pro
                    ? __('Enter the cost of goods for this product to track actual profit in analytics.', 'insightpress-advanced-analytics-for-woocommerce')
                    : __('Actual profit tracking and COGS are available in InsightPress Pro.', 'insightpress-advanced-analytics-for-woocommerce'),
                'desc_tip' => 'true',
                'type' => 'number',
                'custom_attributes' => array(
                    'step' => 'any',
                    'min' => '0',
                    'disabled' => $is_pro ? false : 'disabled'
                )
            )
        );

        echo '</div>';
    }

    /**
     * Save Cost of Goods field.
     */
    public function save_cogs_field($post_id)
    {
        $cogs = isset($_POST['_insightpress_cogs']) ? sanitize_text_field($_POST['_insightpress_cogs']) : '';
        update_post_meta($post_id, '_insightpress_cogs', $cogs);
    }
}
