<?php

/**
 * A1-WooBay - WooCommerce Product Metabox
 *
 * @package A1_WooBay
 * @since   0.3.0
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class A1WooBay_Product_Meta {
    // Core eBay tracking
    const META_PROFILE_ID = '_a1wb_profile_id';

    const META_TEMPLATE_ID = '_a1wb_template_id';

    const META_EBAY_ITEM_ID = '_a1wb_ebay_item_id';

    const META_EBAY_STATUS = '_a1wb_ebay_status';

    const META_EBAY_SYNCED = '_a1wb_ebay_synced_at';

    const META_EBAY_ERROR = '_a1wb_ebay_error';

    const META_OFFER_ID = '_a1wb_ebay_offer_id';

    const META_EBAY_CAT_ID = '_a1wb_ebay_cat_id';

    const META_EBAY_CAT_NAME = '_a1wb_ebay_cat_name';

    const META_NEEDS_REVISION = '_a1wb_needs_revision';

    const META_REVISE_ON_UPDATE = '_a1wb_revise_on_update';

    // Listing details (per-product overrides)
    const META_CUSTOM_TITLE = '_a1wb_custom_title';

    const META_CONDITION_DESC = '_a1wb_condition_desc';

    // Product identifiers
    const META_UPC = '_a1wb_upc';

    const META_MPN = '_a1wb_mpn';

    const META_BRAND = '_a1wb_brand';

    const META_ITEM_SPECIFICS = '_a1wb_item_specifics';

    const META_WATCH_COUNT = '_a1wb_watch_count';

    const META_PRICE_TYPE = '_a1wb_price_type';

    const META_PRICE_VALUE = '_a1wb_price_value';

    const META_LISTING_TYPE = '_a1wb_prod_listing_type';

    const META_LISTING_DURATION = '_a1wb_prod_duration';

    const STATUS_NOT_LISTED = 'not_listed';

    const STATUS_VERIFIED = 'verified';

    const STATUS_ACTIVE = 'active';

    const STATUS_ENDED = 'ended';

    const STATUS_SOLD = 'sold';

    const STATUS_ARCHIVED = 'archived';

    const STATUS_ERROR = 'error';

    private static $instance = null;

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

    private function __construct() {
        add_action( 'add_meta_boxes', array($this, 'add_meta_box') );
        add_action(
            'save_post_product',
            array($this, 'save_meta_box'),
            10,
            2
        );
        add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') );
        add_action( 'woocommerce_update_product', array($this, 'maybe_flag_needs_revision') );
        add_action( 'post_submitbox_misc_actions', array($this, 'publish_box_ebay_status') );
        add_action( 'woocommerce_product_set_stock', array($this, 'maybe_end_listing_on_stock_change') );
    }

    public function enqueue_scripts( $hook ) {
        if ( 'post.php' !== $hook && 'post-new.php' !== $hook ) {
            return;
        }
        global $post;
        if ( !$post || 'product' !== $post->post_type ) {
            return;
        }
        wp_enqueue_style(
            'a1woobay-product-meta',
            A1WOOBAY_PLUGIN_URL . 'assets/css/product-meta.css',
            array(),
            A1WOOBAY_VERSION
        );
        wp_enqueue_script(
            'a1woobay-product-meta',
            A1WOOBAY_PLUGIN_URL . 'assets/js/product-meta.js',
            array('jquery'),
            A1WOOBAY_VERSION,
            true
        );
        wp_enqueue_script(
            'a1woobay-product-meta-publish-box',
            A1WOOBAY_PLUGIN_URL . 'assets/js/product-meta-publish-box.js',
            array('jquery', 'a1woobay-product-meta'),
            A1WOOBAY_VERSION,
            true
        );
        wp_localize_script( 'a1woobay-product-meta', 'a1woobay', array(
            'ajax_url'     => admin_url( 'admin-ajax.php' ),
            'nonce'        => wp_create_nonce( 'a1woobay_product_action' ),
            'search_nonce' => wp_create_nonce( 'a1woobay_admin_ajax' ),
            'product_id'   => $post->ID,
            'i18n'         => array(
                'saving'         => __( 'Saving…', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
                'saved'          => __( 'Saved', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
                'failed'         => __( 'Failed', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
                'requestFailed'  => __( 'Request failed', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
                'working'        => __( 'Working…', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
                'contactingEbay' => __( 'Contacting eBay…', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
                'done'           => __( 'Done!', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
                'actionFailed'   => __( 'Action failed', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
            ),
        ) );
    }

    public function add_meta_box() {
        add_meta_box(
            'a1woobay-product-panel',
            __( 'A1 Marketplace Sync with eBay for WooCommerce &mdash; eBay Listing', 'a1-marketplace-sync-with-ebay-for-woocommerce' ),
            array($this, 'render_meta_box'),
            'product',
            'normal',
            'high'
        );
    }

    /**
     * Fired by woocommerce_update_product after a product is saved.
     * If the listing is currently active, flag it as needing revision.
     */
    public function maybe_flag_needs_revision( $product_id ) {
        if ( self::STATUS_ACTIVE !== self::get_status( $product_id ) ) {
            return;
        }
        self::flag_needs_revision( $product_id );
        // If "Revise on update" is checked, push changes to eBay automatically.
        if ( get_post_meta( (int) $product_id, self::META_REVISE_ON_UPDATE, true ) ) {
            if ( class_exists( 'A1WooBay_Ajax' ) ) {
                A1WooBay_Ajax::do_revise( (int) $product_id );
            }
        }
    }

    /**
     * Auto-end eBay listing when WooCommerce stock reaches 0.
     * Fires on `woocommerce_product_set_stock` (receives WC_Product object).
     *
     * @param WC_Product $product The product whose stock just changed.
     */
    public function maybe_end_listing_on_stock_change( $product ) {
        $product_id = $product->get_id();
        // Only act on active listings.
        if ( self::STATUS_ACTIVE !== self::get_status( $product_id ) ) {
            return;
        }
        // End if stock quantity is 0 or the status flipped to outofstock.
        $qty = $product->get_stock_quantity();
        $stock_status = $product->get_stock_status();
        $is_zero = null !== $qty && (int) $qty <= 0;
        $is_outofstock = 'outofstock' === $stock_status;
        if ( !$is_zero && !$is_outofstock ) {
            return;
        }
        if ( class_exists( 'A1WooBay_Ajax' ) ) {
            A1WooBay_Ajax::do_end_listing( $product_id );
        }
    }

    public function render_meta_box( $post ) {
        wp_nonce_field( 'a1woobay_product_meta', 'a1woobay_product_nonce' );
        // --- Load all meta ---
        $profile_id = get_post_meta( $post->ID, self::META_PROFILE_ID, true );
        $template_id = get_post_meta( $post->ID, self::META_TEMPLATE_ID, true );
        $ebay_item_id = get_post_meta( $post->ID, self::META_EBAY_ITEM_ID, true );
        $ebay_status = ( get_post_meta( $post->ID, self::META_EBAY_STATUS, true ) ?: self::STATUS_NOT_LISTED );
        $synced_at = get_post_meta( $post->ID, self::META_EBAY_SYNCED, true );
        $ebay_error = get_post_meta( $post->ID, self::META_EBAY_ERROR, true );
        $cat_id = get_post_meta( $post->ID, self::META_EBAY_CAT_ID, true );
        $cat_name = get_post_meta( $post->ID, self::META_EBAY_CAT_NAME, true );
        $needs_revision = self::needs_revision( $post->ID );
        // Listing details
        $custom_title = get_post_meta( $post->ID, self::META_CUSTOM_TITLE, true );
        $condition = get_post_meta( $post->ID, '_a1wb_ebay_condition', true );
        $condition_desc = get_post_meta( $post->ID, self::META_CONDITION_DESC, true );
        // Price + listing overrides
        $price_type = get_post_meta( $post->ID, self::META_PRICE_TYPE, true );
        $price_value = get_post_meta( $post->ID, self::META_PRICE_VALUE, true );
        $prod_listing_type = get_post_meta( $post->ID, self::META_LISTING_TYPE, true );
        $prod_duration = get_post_meta( $post->ID, self::META_LISTING_DURATION, true );
        // Product identifiers
        $upc = get_post_meta( $post->ID, self::META_UPC, true );
        $mpn = get_post_meta( $post->ID, self::META_MPN, true );
        $brand = get_post_meta( $post->ID, self::META_BRAND, true );
        $item_specifics = self::get_item_specifics( $post->ID );
        $profiles = A1WooBay_Profiles::get_dropdown_options();
        // Description Templates are a Pro-only feature (WordPress.org
        // Guideline 5) - physically absent from the free/wp.org build.
        $templates = array();
        $auth = A1WooBay_Auth::get_instance();
        $connected = $auth->is_connected();
        // Title truncation preview
        $source_title = ( $custom_title ?: $post->post_title );
        $title_len = mb_strlen( $source_title );
        $title_over = $title_len > 80;
        $truncated_title = '';
        if ( $title_over ) {
            $t = mb_substr( $source_title, 0, 80 );
            $s = mb_strrpos( $t, ' ' );
            if ( $s > 50 ) {
                $t = mb_substr( $t, 0, $s );
            }
            $truncated_title = rtrim( $t );
        }
        // Section open/closed defaults
        $ebay_opts_open = ( $price_type || $prod_listing_type || $prod_duration || $condition || $condition_desc ? 'open' : '' );
        $ident_open = ( $upc || $mpn || $brand ? 'open' : '' );
        $cat_open = ( $cat_id || !empty( $item_specifics ) ? 'open' : '' );
        $status_map = array(
            self::STATUS_NOT_LISTED => array('Prepared', '#888', '#f5f5f5'),
            self::STATUS_VERIFIED   => array('Verified', '#0073aa', '#e8f4fb'),
            self::STATUS_ACTIVE     => array('Published', '#46b450', '#edfaef'),
            self::STATUS_ENDED      => array('Ended', '#dc3232', '#fef0f0'),
            self::STATUS_SOLD       => array('Sold', '#8b5cf6', '#f5f0ff'),
            self::STATUS_ARCHIVED   => array('Archived', '#555', '#f0f0f0'),
            self::STATUS_ERROR      => array('Error', '#dc3232', '#fef0f0'),
        );
        $si = $status_map[$ebay_status] ?? $status_map[self::STATUS_NOT_LISTED];
        ?>
		<!-- Status bar -->
		<div style="margin-bottom:10px;padding:8px 10px;background:<?php 
        echo esc_attr( $si[2] );
        ?>;border-left:3px solid <?php 
        echo esc_attr( $si[1] );
        ?>;border-radius:3px;font-size:12px;">
			<strong style="color:<?php 
        echo esc_attr( $si[1] );
        ?>;"><?php 
        echo esc_html( $si[0] );
        ?></strong>
			<?php 
        if ( $ebay_item_id ) {
            ?>
				&nbsp;&#x2014; Item: <a href="https://www.ebay.com/itm/<?php 
            echo esc_attr( $ebay_item_id );
            ?>" target="_blank" rel="noopener"><?php 
            echo esc_html( $ebay_item_id );
            ?></a>
			<?php 
        }
        ?>
			<?php 
        if ( $synced_at ) {
            ?>
				<span style="float:right;color:#888;">Synced <?php 
            echo esc_html( gmdate( 'M j Y g:ia', (int) $synced_at ) );
            ?></span>
			<?php 
        }
        ?>
			<?php 
        if ( $ebay_error && self::STATUS_ERROR === $ebay_status ) {
            ?>
				<br><span style="color:#dc3232;"><?php 
            echo esc_html( $ebay_error );
            ?></span>
			<?php 
        }
        ?>
		</div>

		<?php 
        if ( $needs_revision && self::STATUS_ACTIVE === $ebay_status ) {
            ?>
		<div style="margin-bottom:8px;padding:7px 10px;background:#fff8e1;border-left:3px solid #d97706;border-radius:3px;font-size:12px;">
			&#9998; <strong style="color:#92400e;"><?php 
            esc_html_e( 'Verification needed', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?></strong>
			&mdash; <?php 
            esc_html_e( 'Product changed since last eBay sync. Click Revise Listing to verify and push.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?>
		</div>
		<?php 
        }
        ?>

		<!-- ========== SECTION: Listing Details ========== -->
		<details class="a1wb-section" open>
			<summary><?php 
        esc_html_e( 'Listing Details', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></summary>
			<div class="a1wb-section-body">

				<div class="a1wb-field">
					<label for="a1wb_custom_title">
						<?php 
        esc_html_e( 'Listing Title', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?>
						<span class="a1wb-hint"><?php 
        esc_html_e( '(leave blank to use product name)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span>
						<span class="a1wb-counter" id="a1wb-title-counter"><?php 
        echo esc_html( mb_strlen( $custom_title ) );
        ?>/80</span>
					</label>
					<input type="text" id="a1wb_custom_title" name="a1wb_custom_title"
						value="<?php 
        echo esc_attr( $custom_title );
        ?>"
						placeholder="<?php 
        echo esc_attr( $post->post_title );
        ?>"
						maxlength="160">
					<?php 
        if ( $title_over && $custom_title ) {
            ?>
						<span class="a1wb-note" style="color:#d97706;">
							&#9998; <?php 
            esc_html_e( 'Will be trimmed to:', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?> <em><?php 
            echo esc_html( $truncated_title );
            ?></em> (<?php 
            echo esc_html( mb_strlen( $truncated_title ) );
            ?> <?php 
            esc_html_e( 'chars', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?>)
						</span>
					<?php 
        } elseif ( $title_over && !$custom_title ) {
            ?>
						<span class="a1wb-note" style="color:#d97706;">
							&#9998; <?php 
            esc_html_e( 'Product name is too long. Will be trimmed to:', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?> <em><?php 
            echo esc_html( $truncated_title );
            ?></em>
						</span>
					<?php 
        } else {
            ?>
						<span class="a1wb-note"><?php 
            echo esc_html( mb_strlen( $source_title ) );
            ?>/80 <?php 
            esc_html_e( 'chars', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?></span>
					<?php 
        }
        ?>
				</div>

				<div class="a1wb-field">
					<label for="a1wb_profile_id"><?php 
        esc_html_e( 'eBay Profile', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
					<select id="a1wb_profile_id" name="a1wb_profile_id">
						<?php 
        foreach ( $profiles as $pid => $plabel ) {
            ?>
							<option value="<?php 
            echo esc_attr( $pid );
            ?>" <?php 
            selected( (int) $profile_id, (int) $pid );
            ?>><?php 
            echo esc_html( $plabel );
            ?></option>
						<?php 
        }
        ?>
					</select>
					<span class="a1wb-note"><a href="<?php 
        echo esc_url( add_query_arg( 'page', 'a1woobay-profiles', admin_url( 'admin.php' ) ) );
        ?>"><?php 
        esc_html_e( 'Manage profiles', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></a></span>
				</div>

				<div class="a1wb-field">
					<label for="a1wb_template_id"><?php 
        esc_html_e( 'Description Template', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
					<?php 
        // Description Templates are a Pro-only feature (WordPress.org
        // Guideline 5) - physically absent from the free/wp.org build.
        // Must be an isolated is__premium_only() condition, see
        // includes/class-a1woobay-templates__premium_only.php's docblock.
        ?>
					<?php 
        ?>
						<p class="a1wb-note"><?php 
        esc_html_e( 'Templates are a Pro feature.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?>
							<a href="<?php 
        echo esc_url( admin_url( 'admin.php?page=a1woobay-settings-pricing' ) );
        ?>"><?php 
        esc_html_e( 'Upgrade to A1 Marketplace Sync with eBay for WooCommerce Pro', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></a>
						</p>
					<?php 
        ?>
				</div>

			</div>
		</details>

		<!-- ========== SECTION: eBay Options ========== -->
		<details class="a1wb-section" <?php 
        echo esc_attr( $ebay_opts_open );
        ?>>
			<summary><?php 
        esc_html_e( 'eBay Options', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?> <span style="font-weight:normal;color:#888;font-size:11px;"><?php 
        esc_html_e( '(all override profile)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span></summary>
			<div class="a1wb-section-body">

				<div class="a1wb-field">
					<label for="a1wb_price_type"><?php 
        esc_html_e( 'Price Source', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
					<select id="a1wb_price_type" name="a1wb_price_type">
						<option value="" <?php 
        selected( $price_type, '' );
        ?>><?php 
        esc_html_e( '-- Use profile setting --', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
						<option value="regular_price" <?php 
        selected( $price_type, 'regular_price' );
        ?>><?php 
        esc_html_e( 'Regular price', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
						<option value="sale_price" <?php 
        selected( $price_type, 'sale_price' );
        ?>><?php 
        esc_html_e( 'Sale price (if set, else regular)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
						<option value="fixed" <?php 
        selected( $price_type, 'fixed' );
        ?>><?php 
        esc_html_e( 'Custom fixed price', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
					</select>
				</div>

				<div class="a1wb-field" id="a1wb-price-value-row" style="<?php 
        echo ( 'fixed' !== $price_type ? 'display:none;' : '' );
        ?>">
					<label for="a1wb_price_value"><?php 
        esc_html_e( 'Custom Price ($)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
					<input type="number" id="a1wb_price_value" name="a1wb_price_value" step="0.01" min="0"
						value="<?php 
        echo esc_attr( $price_value );
        ?>"
						placeholder="e.g. 24.99">
				</div>

				<div class="a1wb-field">
					<label for="a1wb_prod_listing_type"><?php 
        esc_html_e( 'Listing Type', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
					<select id="a1wb_prod_listing_type" name="a1wb_prod_listing_type">
						<option value="" <?php 
        selected( $prod_listing_type, '' );
        ?>><?php 
        esc_html_e( '-- Use profile setting --', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
						<option value="FIXED_PRICE" <?php 
        selected( $prod_listing_type, 'FIXED_PRICE' );
        ?>><?php 
        esc_html_e( 'Fixed Price', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
					</select>
				</div>

				<div class="a1wb-field">
					<label for="a1wb_prod_duration"><?php 
        esc_html_e( 'Listing Duration', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
					<select id="a1wb_prod_duration" name="a1wb_prod_duration">
						<option value="" <?php 
        selected( $prod_duration, '' );
        ?>><?php 
        esc_html_e( '-- Use profile setting --', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
						<?php 
        foreach ( A1WooBay_Profiles::DURATIONS as $dval => $dlabel ) {
            ?>
							<option value="<?php 
            echo esc_attr( $dval );
            ?>" <?php 
            selected( $prod_duration, $dval );
            ?>><?php 
            echo esc_html( $dlabel );
            ?></option>
						<?php 
        }
        ?>
					</select>
				</div>

				<div class="a1wb-field">
					<label for="a1wb_product_condition"><?php 
        esc_html_e( 'Condition', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
					<select id="a1wb_product_condition" name="a1wb_product_condition">
						<option value=""><?php 
        esc_html_e( '-- Use profile setting --', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
						<?php 
        foreach ( A1WooBay_Profiles::CONDITIONS as $cval => $clabel ) {
            if ( '' === $cval ) {
                continue;
            }
            ?>
							<option value="<?php 
            echo esc_attr( $cval );
            ?>" <?php 
            selected( $condition, $cval );
            ?>><?php 
            echo esc_html( $clabel );
            ?></option>
						<?php 
        }
        ?>
					</select>
				</div>

				<div class="a1wb-field">
					<label for="a1wb_condition_desc">
						<?php 
        esc_html_e( 'Condition Description', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?>
						<span class="a1wb-hint"><?php 
        esc_html_e( '(optional, for used items)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span>
					</label>
					<textarea id="a1wb_condition_desc" name="a1wb_condition_desc" rows="2"
						placeholder="<?php 
        esc_attr_e( 'e.g. All pieces present, box has minor shelf wear.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?>"
					><?php 
        echo esc_textarea( $condition_desc );
        ?></textarea>
				</div>

			</div>
		</details>

		<!-- ========== SECTION: Product Identifiers ========== -->
		<details class="a1wb-section" <?php 
        echo esc_attr( $ident_open );
        ?>>
			<summary><?php 
        esc_html_e( 'Product Identifiers', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></summary>
			<div class="a1wb-section-body">

				<div class="a1wb-field">
					<label for="a1wb_brand"><?php 
        esc_html_e( 'Brand', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
					<input type="text" id="a1wb_brand" name="a1wb_brand"
						value="<?php 
        echo esc_attr( $brand );
        ?>"
						placeholder="e.g. SunsOut, Ravensburger">
				</div>

				<div class="a1wb-field">
					<label for="a1wb_mpn"><?php 
        esc_html_e( 'MPN', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?> <span class="a1wb-hint"><?php 
        esc_html_e( '(Manufacturer Part Number)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span></label>
					<input type="text" id="a1wb_mpn" name="a1wb_mpn"
						value="<?php 
        echo esc_attr( $mpn );
        ?>"
						placeholder="e.g. 57161">
				</div>

				<div class="a1wb-field">
					<label for="a1wb_upc"><?php 
        esc_html_e( 'UPC', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?> <span class="a1wb-hint"><?php 
        esc_html_e( '(barcode / GTIN)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span></label>
					<input type="text" id="a1wb_upc" name="a1wb_upc"
						value="<?php 
        echo esc_attr( $upc );
        ?>"
						placeholder="e.g. 796780571617">
					<span class="a1wb-note"><?php 
        esc_html_e( 'eBay uses GTINs to match your listing to the product catalog, improving search visibility.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span>
				</div>

			</div>
		</details>

		<!-- ========== SECTION: Category & Specifics ========== -->
		<details class="a1wb-section" <?php 
        echo esc_attr( $cat_open );
        ?>>
			<summary><?php 
        esc_html_e( 'eBay Category &amp; Specifics', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?> <span style="font-weight:normal;color:#888;font-size:11px;"><?php 
        esc_html_e( '(both override profile)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span></summary>
			<div class="a1wb-section-body">

				<div class="a1wb-field">
					<?php 
        if ( $cat_id && $cat_name ) {
            ?>
						<div id="a1wb-prod-cat-selected" style="margin-bottom:6px;padding:4px 8px;background:#e8f4fb;border:1px solid #b3d7ee;border-radius:3px;font-size:12px;">
							<strong><?php 
            echo esc_html( $cat_name );
            ?></strong>
							<span style="color:#888;"> (ID: <?php 
            echo esc_html( $cat_id );
            ?>)</span>
							<a href="#" id="a1wb-prod-cat-clear" style="float:right;color:#dc3232;text-decoration:none;" title="Clear">&times;</a>
						</div>
					<?php 
        } else {
            ?>
						<div id="a1wb-prod-cat-selected" style="display:none;margin-bottom:6px;padding:4px 8px;background:#e8f4fb;border:1px solid #b3d7ee;border-radius:3px;font-size:12px;"></div>
					<?php 
        }
        ?>
					<input type="hidden" id="a1wb_ebay_cat_id"   name="a1wb_ebay_cat_id"   value="<?php 
        echo esc_attr( $cat_id );
        ?>">
					<input type="hidden" id="a1wb_ebay_cat_name" name="a1wb_ebay_cat_name" value="<?php 
        echo esc_attr( $cat_name );
        ?>">
					<div style="display:flex;gap:4px;">
						<input type="text" id="a1wb-prod-cat-search" placeholder="<?php 
        esc_attr_e( 'Search eBay categories…', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?>" style="flex:1;min-width:0;font-size:12px;" autocomplete="off">
						<button type="button" id="a1wb-prod-cat-btn" class="button" style="font-size:12px;padding:0 8px;"><?php 
        esc_html_e( 'Search', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></button>
					</div>
					<div id="a1wb-prod-cat-results" style="display:none;max-height:160px;overflow-y:auto;margin-top:4px;border:1px solid #ddd;border-radius:3px;background:#fff;font-size:12px;"></div>
				<?php 
        if ( $cat_id ) {
            ?>
				<div id="a1wb-aspects-bar" style="margin-top:6px;">
				<?php 
        } else {
            ?>
				<div id="a1wb-aspects-bar" style="margin-top:6px;display:none;">
				<?php 
        }
        ?>
					<div style="display:flex;gap:4px;align-items:center;flex-wrap:wrap;">
						<button type="button" id="a1wb-load-aspects-btn" class="button" style="font-size:12px;padding:0 8px;"><?php 
        esc_html_e( 'Load Suggested Specifics', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></button>
						<button type="button" id="a1wb-browse-aspects-btn" class="button" style="font-size:12px;padding:0 8px;"><?php 
        esc_html_e( 'Browse All', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></button>
						<span id="a1wb-aspects-msg" style="font-size:11px;color:#888;"></span>
					</div>
					<div id="a1wb-aspects-browser" style="display:none;margin-top:6px;border:1px solid #ddd;border-radius:3px;background:#fafafa;padding:8px;">
						<div style="display:flex;gap:4px;margin-bottom:6px;align-items:center;">
							<input type="text" id="a1wb-aspects-search" placeholder="<?php 
        esc_attr_e( 'Search specifics...', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?>" style="flex:1;font-size:12px;">
							<select id="a1wb-aspects-filter" style="font-size:12px;">
								<option value="all"><?php 
        esc_html_e( 'All', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
								<option value="REQUIRED"><?php 
        esc_html_e( 'Required', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
								<option value="RECOMMENDED"><?php 
        esc_html_e( 'Recommended', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
								<option value="OPTIONAL"><?php 
        esc_html_e( 'Optional', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
							</select>
							<button type="button" id="a1wb-aspects-close" class="button" style="font-size:12px;padding:0 8px;"><?php 
        esc_html_e( 'Close', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></button>
						</div>
						<div id="a1wb-aspects-list" style="max-height:260px;overflow-y:auto;"></div>
					</div>
				</div>
				</div>


				<hr style="margin:10px 0;border-color:#eee;">
				<div class="a1wb-field">
					<label style="margin-bottom:6px;"><?php 
        esc_html_e( 'Item Specifics', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?> <span class="a1wb-hint"><?php 
        esc_html_e( '(supplements profile specifics)', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span></label>
					<table style="width:100%;border-collapse:collapse;">
						<thead><tr style="font-size:11px;color:#888;">
							<th style="text-align:left;padding:2px 4px;width:32%;"><?php 
        esc_html_e( 'Name', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></th>
							<th style="text-align:left;padding:2px 4px;width:28%;"><?php 
        esc_html_e( 'Source', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></th>
							<th style="text-align:left;padding:2px 4px;"><?php 
        esc_html_e( 'Value / Attribute', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></th>
							<th style="width:26px;"></th>
						</tr></thead>
						<tbody id="a1wb-spec-tbody">
						<?php 
        foreach ( $item_specifics as $idx => $spec ) {
            ?>
						<tr class="a1wb-spec-row">
							<td style="padding:2px 4px;"><input type="text" name="a1wb_specifics[<?php 
            echo esc_attr( $idx );
            ?>][name]" value="<?php 
            echo esc_attr( $spec['name'] ?? '' );
            ?>" placeholder="e.g. Pattern" style="width:100%;font-size:12px;"></td>
							<td style="padding:2px 4px;"><select name="a1wb_specifics[<?php 
            echo esc_attr( $idx );
            ?>][source]" class="a1wb-spec-source" style="width:100%;font-size:12px;"><option value="custom" <?php 
            selected( $spec['source'] ?? 'custom', 'custom' );
            ?>><?php 
            esc_html_e( 'Custom', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?></option><option value="attribute" <?php 
            selected( $spec['source'] ?? 'custom', 'attribute' );
            ?>><?php 
            esc_html_e( 'WC Attribute', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?></option></select></td>
							<td style="padding:2px 4px;"><input type="text" name="a1wb_specifics[<?php 
            echo esc_attr( $idx );
            ?>][value]" value="<?php 
            echo esc_attr( $spec['value'] ?? '' );
            ?>" placeholder="<?php 
            echo ( 'attribute' === ($spec['source'] ?? 'custom') ? esc_attr__( 'e.g. pa_pattern', 'a1-marketplace-sync-with-ebay-for-woocommerce' ) : esc_attr__( 'e.g. Floral', 'a1-marketplace-sync-with-ebay-for-woocommerce' ) );
            ?>" style="width:100%;font-size:12px;"></td>
							<td style="padding:2px 4px;"><button type="button" class="button a1wb-spec-remove" style="padding:0 5px;color:#dc3232;min-width:0;">&times;</button></td>
						</tr>
						<?php 
        }
        ?>
						</tbody>
					</table>
					<button type="button" id="a1wb-spec-add" class="button" style="margin-top:6px;font-size:12px;">+ <?php 
        esc_html_e( 'Add Specific', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></button>
					<span class="a1wb-note"><?php 
        esc_html_e( 'Merge with profile specifics — product value wins on conflict.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span>
				</div>
			</div>
		</details>

		<hr style="margin:10px 0;">

		<!-- ========== ACTION BUTTONS ========== -->
		<?php 
        if ( !$connected ) {
            ?>
			<p style="color:#dc3232;font-size:12px;"><?php 
            esc_html_e( 'Not connected to eBay. Connect in A1 Marketplace Sync with eBay for WooCommerce > Settings.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?></p>
		<?php 
        } elseif ( !$profile_id ) {
            ?>
			<p style="color:#888;font-size:12px;"><?php 
            esc_html_e( 'Assign a profile above to enable eBay actions.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?></p>
		<?php 
        } else {
            ?>
			<div id="a1wb-push-result" style="margin-bottom:8px;font-size:12px;"></div>
			<?php 
            if ( !A1WooBay_License::allow_image_sync() ) {
                ?>
				<p style="font-size:11px;color:#888;margin:0 0 6px;">
					🔒 <?php 
                esc_html_e( 'Images:', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?>
					<a href="<?php 
                echo esc_url( admin_url( 'admin.php?page=a1woobay-settings-pricing' ) );
                ?>"><?php 
                esc_html_e( 'Pro feature', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?></a>
					— <?php 
                esc_html_e( 'listings will publish without photos on the free plan.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?>
				</p>
			<?php 
            }
            ?>
			<div style="display:flex;gap:8px;flex-wrap:wrap;">
			<?php 
            if ( in_array( $ebay_status, array(self::STATUS_NOT_LISTED, self::STATUS_ERROR), true ) ) {
                ?>
				<button type="button" id="a1wb-verify-btn" class="button button-primary" data-product-id="<?php 
                echo esc_attr( $post->ID );
                ?>">
					<?php 
                esc_html_e( 'Verify with eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?>
				</button>
			<?php 
            } elseif ( self::STATUS_VERIFIED === $ebay_status ) {
                ?>
				<button type="button" id="a1wb-push-btn" class="button button-primary" data-product-id="<?php 
                echo esc_attr( $post->ID );
                ?>">
					<?php 
                echo ( $ebay_item_id ? esc_html__( 'Revise Listing', 'a1-marketplace-sync-with-ebay-for-woocommerce' ) : esc_html__( 'Publish to eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' ) );
                ?>
				</button>
				<button type="button" id="a1wb-verify-btn" class="button" data-product-id="<?php 
                echo esc_attr( $post->ID );
                ?>">
					<?php 
                esc_html_e( 'Re-Verify', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?>
				</button>
			<?php 
            } elseif ( self::STATUS_ACTIVE === $ebay_status ) {
                ?>
				<button type="button" id="a1wb-verify-btn" class="button button-primary" data-product-id="<?php 
                echo esc_attr( $post->ID );
                ?>">
					<?php 
                esc_html_e( 'Verify with eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?>
				</button>
				<button type="button" id="a1wb-end-btn" class="button" style="color:#dc3232;" data-product-id="<?php 
                echo esc_attr( $post->ID );
                ?>">
					<?php 
                esc_html_e( 'End Listing', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?>
				</button>
				<button type="button" id="a1wb-sold-btn" class="button" style="color:#8b5cf6;" data-product-id="<?php 
                echo esc_attr( $post->ID );
                ?>">
					<?php 
                esc_html_e( 'Mark as Sold', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?>
				</button>
			<?php 
            } elseif ( self::STATUS_ENDED === $ebay_status || self::STATUS_SOLD === $ebay_status ) {
                ?>
				<button type="button" id="a1wb-relist-btn" class="button button-primary" data-product-id="<?php 
                echo esc_attr( $post->ID );
                ?>">
					<?php 
                esc_html_e( 'Re-list on eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?>
				</button>
			<?php 
            }
            ?>
			</div>
		<?php 
        }
        ?>
		<?php 
    }

    public function save_meta_box( $post_id, $post ) {
        if ( !isset( $_POST['a1woobay_product_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['a1woobay_product_nonce'] ) ), 'a1woobay_product_meta' ) ) {
            return;
        }
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
        if ( !current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
        // Profile & template
        update_post_meta( $post_id, self::META_PROFILE_ID, absint( wp_unslash( $_POST['a1wb_profile_id'] ?? 0 ) ) );
        update_post_meta( $post_id, self::META_TEMPLATE_ID, absint( wp_unslash( $_POST['a1wb_template_id'] ?? 0 ) ) );
        // Custom title
        $custom_title = sanitize_text_field( wp_unslash( $_POST['a1wb_custom_title'] ?? '' ) );
        if ( $custom_title ) {
            update_post_meta( $post_id, self::META_CUSTOM_TITLE, $custom_title );
        } else {
            delete_post_meta( $post_id, self::META_CUSTOM_TITLE );
        }
        // Price override
        $price_type = sanitize_text_field( wp_unslash( $_POST['a1wb_price_type'] ?? '' ) );
        $price_value = (float) sanitize_text_field( wp_unslash( $_POST['a1wb_price_value'] ?? '' ) );
        $allowed_price_types = array(
            '',
            'regular_price',
            'sale_price',
            'fixed'
        );
        if ( in_array( $price_type, $allowed_price_types, true ) ) {
            if ( $price_type ) {
                update_post_meta( $post_id, self::META_PRICE_TYPE, $price_type );
            } else {
                delete_post_meta( $post_id, self::META_PRICE_TYPE );
            }
            if ( 'fixed' === $price_type && $price_value > 0 ) {
                update_post_meta( $post_id, self::META_PRICE_VALUE, (string) $price_value );
            } else {
                delete_post_meta( $post_id, self::META_PRICE_VALUE );
            }
        }
        // Listing type + duration overrides
        $prod_listing_type = sanitize_text_field( wp_unslash( $_POST['a1wb_prod_listing_type'] ?? '' ) );
        $prod_duration_val = sanitize_text_field( wp_unslash( $_POST['a1wb_prod_duration'] ?? '' ) );
        if ( $prod_listing_type ) {
            update_post_meta( $post_id, self::META_LISTING_TYPE, $prod_listing_type );
        } else {
            delete_post_meta( $post_id, self::META_LISTING_TYPE );
        }
        if ( $prod_duration_val ) {
            update_post_meta( $post_id, self::META_LISTING_DURATION, $prod_duration_val );
        } else {
            delete_post_meta( $post_id, self::META_LISTING_DURATION );
        }
        // Condition
        $prod_cond = sanitize_text_field( wp_unslash( $_POST['a1wb_product_condition'] ?? '' ) );
        if ( $prod_cond ) {
            update_post_meta( $post_id, '_a1wb_ebay_condition', $prod_cond );
        } else {
            delete_post_meta( $post_id, '_a1wb_ebay_condition' );
        }
        // Condition description
        $cond_desc = sanitize_textarea_field( wp_unslash( $_POST['a1wb_condition_desc'] ?? '' ) );
        if ( $cond_desc ) {
            update_post_meta( $post_id, self::META_CONDITION_DESC, $cond_desc );
        } else {
            delete_post_meta( $post_id, self::META_CONDITION_DESC );
        }
        // Product identifiers
        $brand = sanitize_text_field( wp_unslash( $_POST['a1wb_brand'] ?? '' ) );
        $mpn = sanitize_text_field( wp_unslash( $_POST['a1wb_mpn'] ?? '' ) );
        $upc = sanitize_text_field( wp_unslash( $_POST['a1wb_upc'] ?? '' ) );
        if ( $brand ) {
            update_post_meta( $post_id, self::META_BRAND, $brand );
        } else {
            delete_post_meta( $post_id, self::META_BRAND );
        }
        if ( $mpn ) {
            update_post_meta( $post_id, self::META_MPN, $mpn );
        } else {
            delete_post_meta( $post_id, self::META_MPN );
        }
        if ( $upc ) {
            update_post_meta( $post_id, self::META_UPC, $upc );
        } else {
            delete_post_meta( $post_id, self::META_UPC );
        }
        // Category
        $cat_id = sanitize_text_field( wp_unslash( $_POST['a1wb_ebay_cat_id'] ?? '' ) );
        $cat_name = sanitize_text_field( wp_unslash( $_POST['a1wb_ebay_cat_name'] ?? '' ) );
        if ( $cat_id ) {
            update_post_meta( $post_id, self::META_EBAY_CAT_ID, $cat_id );
            update_post_meta( $post_id, self::META_EBAY_CAT_NAME, $cat_name );
        } else {
            delete_post_meta( $post_id, self::META_EBAY_CAT_ID );
            delete_post_meta( $post_id, self::META_EBAY_CAT_NAME );
        }
        // Per-product item specifics
        $raw_specs = ( isset( $_POST['a1wb_specifics'] ) ? map_deep( wp_unslash( $_POST['a1wb_specifics'] ), 'sanitize_text_field' ) : array() );
        $specs = array();
        foreach ( $raw_specs as $spec ) {
            $name = $spec['name'] ?? '';
            $source = $spec['source'] ?? 'custom';
            $value = $spec['value'] ?? '';
            if ( $name && $value ) {
                $specs[] = array(
                    'name'   => $name,
                    'source' => $source,
                    'value'  => $value,
                );
            }
        }
        if ( $specs ) {
            update_post_meta( $post_id, self::META_ITEM_SPECIFICS, $specs );
        } else {
            delete_post_meta( $post_id, self::META_ITEM_SPECIFICS );
        }
        // Revise-on-update checkbox (from Publish sidebar)
        update_post_meta( $post_id, self::META_REVISE_ON_UPDATE, ( isset( $_POST['a1wb_revise_on_update'] ) ? '1' : '' ) );
    }

    public static function get_item_specifics( $product_id ) {
        $val = get_post_meta( (int) $product_id, self::META_ITEM_SPECIFICS, true );
        return ( is_array( $val ) ? $val : array() );
    }

    // --- Status helpers ---
    public static function set_listing_verified( $product_id ) {
        update_post_meta( $product_id, self::META_EBAY_STATUS, self::STATUS_VERIFIED );
        delete_post_meta( $product_id, self::META_EBAY_ERROR );
    }

    public static function set_listing_active( $product_id, $ebay_item_id = null ) {
        if ( null !== $ebay_item_id && '' !== $ebay_item_id ) {
            update_post_meta( $product_id, self::META_EBAY_ITEM_ID, sanitize_text_field( $ebay_item_id ) );
        }
        update_post_meta( $product_id, self::META_EBAY_STATUS, self::STATUS_ACTIVE );
        update_post_meta( $product_id, self::META_EBAY_SYNCED, time() );
        delete_post_meta( $product_id, self::META_EBAY_ERROR );
        self::clear_needs_revision( $product_id );
    }

    public static function set_listing_ended( $product_id ) {
        update_post_meta( $product_id, self::META_EBAY_STATUS, self::STATUS_ENDED );
        update_post_meta( $product_id, self::META_EBAY_SYNCED, time() );
        self::clear_needs_revision( $product_id );
    }

    public static function set_listing_sold( $product_id ) {
        update_post_meta( $product_id, self::META_EBAY_STATUS, self::STATUS_SOLD );
        update_post_meta( $product_id, self::META_EBAY_SYNCED, time() );
        self::clear_needs_revision( $product_id );
    }

    public static function set_listing_archived( $product_id ) {
        update_post_meta( $product_id, self::META_EBAY_STATUS, self::STATUS_ARCHIVED );
        self::clear_needs_revision( $product_id );
    }

    public static function set_listing_error( $product_id, $message ) {
        update_post_meta( $product_id, self::META_EBAY_STATUS, self::STATUS_ERROR );
        update_post_meta( $product_id, self::META_EBAY_ERROR, sanitize_text_field( $message ) );
    }

    public static function reset_to_prepared( $product_id ) {
        update_post_meta( $product_id, self::META_EBAY_STATUS, self::STATUS_NOT_LISTED );
        delete_post_meta( $product_id, self::META_EBAY_ERROR );
        self::clear_needs_revision( $product_id );
    }

    // --- Needs-revision flag ---
    public static function flag_needs_revision( $product_id ) {
        update_post_meta( (int) $product_id, self::META_NEEDS_REVISION, '1' );
    }

    public static function clear_needs_revision( $product_id ) {
        delete_post_meta( (int) $product_id, self::META_NEEDS_REVISION );
    }

    public static function needs_revision( $product_id ) {
        return (bool) get_post_meta( (int) $product_id, self::META_NEEDS_REVISION, true );
    }

    // --- Getters ---
    public static function get_profile_id( $product_id ) {
        return (int) get_post_meta( $product_id, self::META_PROFILE_ID, true );
    }

    public static function get_template_id( $product_id ) {
        return (int) get_post_meta( $product_id, self::META_TEMPLATE_ID, true );
    }

    public static function get_ebay_item_id( $product_id ) {
        return get_post_meta( $product_id, self::META_EBAY_ITEM_ID, true );
    }

    public static function get_status( $product_id ) {
        return ( get_post_meta( $product_id, self::META_EBAY_STATUS, true ) ?: self::STATUS_NOT_LISTED );
    }

    public static function get_status_label( $status ) {
        $labels = array(
            self::STATUS_NOT_LISTED => 'Prepared',
            self::STATUS_VERIFIED   => 'Verified',
            self::STATUS_ACTIVE     => 'Published',
            self::STATUS_ENDED      => 'Ended',
            self::STATUS_SOLD       => 'Sold',
            self::STATUS_ARCHIVED   => 'Archived',
            self::STATUS_ERROR      => 'Error',
        );
        return $labels[$status] ?? $status;
    }

    public static function get_status_color( $status ) {
        $colors = array(
            self::STATUS_NOT_LISTED => '#888',
            self::STATUS_VERIFIED   => '#0073aa',
            self::STATUS_ACTIVE     => '#46b450',
            self::STATUS_ENDED      => '#dc3232',
            self::STATUS_SOLD       => '#8b5cf6',
            self::STATUS_ARCHIVED   => '#555',
            self::STATUS_ERROR      => '#dc3232',
        );
        return $colors[$status] ?? '#888';
    }

    public static function set_offer_id( $product_id, $offer_id ) {
        update_post_meta( $product_id, self::META_OFFER_ID, sanitize_text_field( $offer_id ) );
    }

    public static function get_offer_id( $product_id ) {
        return get_post_meta( $product_id, self::META_OFFER_ID, true );
    }

    public static function get_product_category_id( $product_id ) {
        return get_post_meta( $product_id, self::META_EBAY_CAT_ID, true );
    }

    public static function get_product_category_name( $product_id ) {
        return get_post_meta( $product_id, self::META_EBAY_CAT_NAME, true );
    }

    public static function get_custom_title( $product_id ) {
        return get_post_meta( $product_id, self::META_CUSTOM_TITLE, true );
    }

    public static function get_condition_desc( $product_id ) {
        return get_post_meta( $product_id, self::META_CONDITION_DESC, true );
    }

    public static function get_upc( $product_id ) {
        return get_post_meta( $product_id, self::META_UPC, true );
    }

    public static function get_mpn( $product_id ) {
        return get_post_meta( $product_id, self::META_MPN, true );
    }

    public static function get_brand( $product_id ) {
        return get_post_meta( $product_id, self::META_BRAND, true );
    }

    /**
     * Render eBay status panel inside the WP Publish sidebar box.
     *
     * @param WP_Post $post Current post object.
     */
    public function publish_box_ebay_status( $post ) : void {
        if ( 'product' !== $post->post_type ) {
            return;
        }
        $pid = $post->ID;
        $profile_id = self::get_profile_id( $pid );
        $status = self::get_status( $pid );
        $item_id = self::get_ebay_item_id( $pid );
        $needs_rev = self::needs_revision( $pid );
        $color = self::get_status_color( $status );
        $rou = (bool) get_post_meta( $pid, self::META_REVISE_ON_UPDATE, true );
        // Status phrase (mirrors WP-Lister's "eBay listing is changed")
        $status_phrase = array(
            self::STATUS_NOT_LISTED => 'not yet listed',
            self::STATUS_VERIFIED   => 'verified – ready to publish',
            self::STATUS_ACTIVE     => ( $needs_rev ? 'changed' : 'active' ),
            self::STATUS_ENDED      => 'ended',
            self::STATUS_SOLD       => 'sold',
            self::STATUS_ARCHIVED   => 'archived',
            self::STATUS_ERROR      => 'error',
        );
        $phrase = $status_phrase[$status] ?? $status;
        // Primary action button
        $action = '';
        $action_label = '';
        $action_btn_class = 'button';
        if ( $profile_id ) {
            if ( in_array( $status, array(self::STATUS_NOT_LISTED, self::STATUS_ERROR), true ) ) {
                $action = 'verify';
                $action_label = __( 'Verify with eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                $action_btn_class = 'button button-primary';
            } elseif ( self::STATUS_VERIFIED === $status ) {
                $action = 'publish';
                $action_label = ( $item_id ? __( 'Revise Listing', 'a1-marketplace-sync-with-ebay-for-woocommerce' ) : __( 'Publish to eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' ) );
                $action_btn_class = 'button button-primary';
            } elseif ( self::STATUS_ACTIVE === $status ) {
                $action = 'verify';
                $action_label = __( 'Verify with eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                if ( $needs_rev ) {
                    $action_btn_class = 'button button-primary';
                }
            } elseif ( in_array( $status, array(self::STATUS_ENDED, self::STATUS_SOLD), true ) ) {
                $action = 'publish';
                $action_label = __( 'Re-list on eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                $action_btn_class = 'button button-primary';
            }
        }
        // All available profiles for the switcher
        $profiles = get_posts( array(
            'post_type'      => 'a1woobay_profile',
            'post_status'    => 'publish',
            'posts_per_page' => -1,
            'orderby'        => 'title',
            'order'          => 'ASC',
        ) );
        $pb_nonce = wp_create_nonce( 'a1woobay_product_action' );
        ?>
		<div class="misc-pub-section" id="a1wb-publish-box-panel" style="border-top:1px solid #ddd;padding:8px 0 10px;">
			<span style="display:block;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.06em;color:#888;margin-bottom:6px;"><?php 
        esc_html_e( 'eBay Listing', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></span>

			<?php 
        if ( $profile_id ) {
            ?>
			<div style="display:flex;align-items:baseline;justify-content:space-between;gap:8px;margin-bottom:4px;">
				<span style="font-size:13px;">
					<?php 
            esc_html_e( 'Listing is', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?>
					<strong style="color:<?php 
            echo esc_attr( $color );
            ?>;"><?php 
            echo esc_html( $phrase );
            ?></strong>
				</span>
				<?php 
            if ( $item_id ) {
                ?>
					<a href="https://www.ebay.com/itm/<?php 
                echo esc_attr( $item_id );
                ?>" target="_blank" rel="noopener noreferrer" style="font-size:12px;white-space:nowrap;"><?php 
                esc_html_e( 'View on eBay ↗', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
                ?></a>
				<?php 
            }
            ?>
			</div>
			<?php 
        }
        ?>

			<!-- Profile selector -->
			<div style="margin-bottom:6px;">
				<label style="font-size:12px;display:block;margin-bottom:2px;color:#555;"><?php 
        esc_html_e( 'Profile', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></label>
				<div style="display:flex;gap:6px;align-items:center;">
					<select id="a1wb-pb-profile-select" style="font-size:12px;flex:1;min-width:0;">
						<option value="0"><?php 
        esc_html_e( '— No profile —', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
        ?></option>
						<?php 
        foreach ( $profiles as $p ) {
            ?>
							<option value="<?php 
            echo esc_attr( $p->ID );
            ?>" <?php 
            selected( $p->ID, $profile_id );
            ?>>
								<?php 
            echo esc_html( $p->post_title );
            ?>
							</option>
						<?php 
        }
        ?>
					</select>
					<span id="a1wb-pb-profile-result" style="font-size:11px;color:#888;white-space:nowrap;"></span>
				</div>
			</div>

			<?php 
        if ( $action ) {
            ?>
			<!-- Primary action -->
			<div style="margin-bottom:8px;">
				<button type="button"
					class="<?php 
            echo esc_attr( $action_btn_class );
            ?> a1wb-pb-action"
					data-action="<?php 
            echo esc_attr( $action );
            ?>"
					data-pid="<?php 
            echo esc_attr( $pid );
            ?>"
					data-nonce="<?php 
            echo esc_attr( $pb_nonce );
            ?>"
					data-label="<?php 
            echo esc_attr( $action_label );
            ?>"
					style="font-size:12px;">
					<?php 
            echo esc_html( $action_label );
            ?>
				</button>
				<span class="a1wb-pb-result" style="display:block;font-size:12px;margin-top:4px;"></span>
			</div>
			<?php 
        }
        ?>

			<!-- Revise on update checkbox (saved by WP's product update form) -->
			<?php 
        if ( $profile_id && in_array( $status, array(self::STATUS_ACTIVE, self::STATUS_VERIFIED), true ) ) {
            ?>
			<label style="display:flex;align-items:center;gap:5px;font-size:12px;cursor:pointer;margin-top:4px;">
				<input type="checkbox" name="a1wb_revise_on_update" value="1" <?php 
            checked( $rou );
            ?> style="margin:0;">
				<?php 
            esc_html_e( 'Revise listing on update', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
            ?>
			</label>
			<?php 
        }
        ?>
		</div>
		<?php 
    }

}
