<?php
/**
 * A1-WooBay - Import & Adopt eBay Listings
 *
 * @package A1_WooBay
 * @since   0.5.6
 */

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

$auth      = A1WooBay_Auth::get_instance();
$connected = $auth->is_connected();

// Build WooCommerce product list for the Adopt dropdown
$wc_products = wc_get_products( array(
	'status'  => 'publish',
	'limit'   => 500,
	'orderby' => 'title',
	'order'   => 'ASC',
) );
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Import & Adopt eBay Listings', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></h1>

	<?php if ( ! $connected ) : ?>
		<div class="notice notice-error"><p><?php esc_html_e( 'Not connected to eBay. Connect in A1 Marketplace Sync with eBay for WooCommerce > Settings first.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></p></div>
	<?php else : ?>

	<!-- =========================================================
	     SECTION 1: Scan eBay Inventory API
	     ========================================================= -->
	<div style="background:#fff;border:1px solid #ddd;border-radius:4px;padding:20px;margin-bottom:24px;">
		<h2 style="margin-top:0;"><?php esc_html_e( 'Scan eBay Inventory', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></h2>
		<p style="color:#555;margin-bottom:12px;">
			<?php esc_html_e( 'Pulls all items from the eBay Inventory API and matches them to WooCommerce products by SKU. Items listed through A1 Marketplace Sync with eBay for WooCommerce will appear here automatically.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
		</p>
		<button type="button" id="a1wb-scan-btn" class="button button-primary">
			<?php esc_html_e( 'Scan eBay Inventory', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
		</button>
		<span id="a1wb-scan-spinner" style="display:none;margin-left:10px;color:#888;font-style:italic;">
			<?php esc_html_e( 'Scanning eBay... this may take a moment.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
		</span>

		<div id="a1wb-scan-results" style="margin-top:16px;display:none;">
			<table class="wp-list-table widefat fixed striped">
				<thead>
					<tr>
						<th style="width:130px;"><?php esc_html_e( 'SKU', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'eBay Title', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:180px;"><?php esc_html_e( 'WooCommerce Product', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:110px;"><?php esc_html_e( 'eBay Item ID', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:90px;"><?php esc_html_e( 'eBay Status', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:110px;"><?php esc_html_e( 'Action', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
					</tr>
				</thead>
				<tbody id="a1wb-scan-tbody"></tbody>
			</table>
		</div>

		<div id="a1wb-scan-error" style="display:none;margin-top:12px;padding:8px 12px;background:#fef0f0;border-left:3px solid #dc3232;border-radius:3px;color:#dc3232;"></div>
	</div>

	<!-- =========================================================
	     SECTION 2: Adopt by eBay Item ID (legacy / WP-Lister)
	     ========================================================= -->
	<div style="background:#fff;border:1px solid #ddd;border-radius:4px;padding:20px;margin-bottom:24px;">
		<h2 style="margin-top:0;"><?php esc_html_e( 'Adopt a Listing by Item ID', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></h2>
		<p style="color:#555;margin-bottom:16px;">
			<?php esc_html_e( 'For listings created outside A1 Marketplace Sync with eBay for WooCommerce (e.g. WP-Lister). Enter the eBay Item ID and select the matching WooCommerce product. The listing will be tracked as active — future revisions go through the Inventory API.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
		</p>

		<div style="display:grid;grid-template-columns:1fr 1fr auto;gap:12px;align-items:end;max-width:720px;">
			<div>
				<label for="a1wb-adopt-item-id" style="display:block;font-weight:600;margin-bottom:4px;">
					<?php esc_html_e( 'eBay Item ID', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
				</label>
				<input type="text" id="a1wb-adopt-item-id" placeholder="e.g. 227408888575"
					style="width:100%;" autocomplete="off">
			</div>
			<div>
				<label for="a1wb-adopt-product" style="display:block;font-weight:600;margin-bottom:4px;">
					<?php esc_html_e( 'WooCommerce Product', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
				</label>
				<select id="a1wb-adopt-product" style="width:100%;">
					<option value=""><?php esc_html_e( '-- Select a product --', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></option>
					<?php foreach ( $wc_products as $p ) : ?>
						<option value="<?php echo esc_attr( $p->get_id() ); ?>">
							<?php
							$sku  = $p->get_sku();
							$name = $p->get_name();
							echo esc_html( $name . ( $sku ? ' [' . $sku . ']' : '' ) );
							?>
						</option>
					<?php endforeach; ?>
				</select>
			</div>
			<div>
				<button type="button" id="a1wb-adopt-btn" class="button button-primary" style="height:30px;">
					<?php esc_html_e( 'Adopt Listing', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
				</button>
			</div>
		</div>

		<div id="a1wb-adopt-result" style="margin-top:12px;font-size:13px;"></div>
	</div>

	<!-- =========================================================
	     SECTION 3: Currently Adopted / Tracked Listings
	     ========================================================= -->
	<div style="background:#fff;border:1px solid #ddd;border-radius:4px;padding:20px;">
		<h2 style="margin-top:0;"><?php esc_html_e( 'Tracked Listings', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></h2>
		<?php
		$tracked = new WP_Query( array(
			'post_type'      => 'product',
			'post_status'    => 'publish',
			'posts_per_page' => 200,
			'meta_query'     => array(
				array(
					'key'     => A1WooBay_Product_Meta::META_EBAY_ITEM_ID,
					'compare' => 'EXISTS',
				),
			),
		) );

		if ( ! $tracked->have_posts() ) : ?>
			<p style="color:#888;"><?php esc_html_e( 'No listings tracked yet. Scan or adopt listings above.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></p>
		<?php else : ?>
			<table class="wp-list-table widefat fixed striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Product', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:120px;"><?php esc_html_e( 'SKU', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:130px;"><?php esc_html_e( 'eBay Item ID', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:90px;"><?php esc_html_e( 'Status', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:90px;"><?php esc_html_e( 'Actions', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $tracked->posts as $p ) :
						$product  = wc_get_product( $p->ID );
						if ( ! $product ) continue;
						$item_id  = A1WooBay_Product_Meta::get_ebay_item_id( $p->ID );
						$status   = A1WooBay_Product_Meta::get_status( $p->ID );
						$label    = A1WooBay_Product_Meta::get_status_label( $status );
						$color    = A1WooBay_Product_Meta::get_status_color( $status );
						$edit_url = get_edit_post_link( $p->ID );
					?>
					<tr>
						<td>
							<a href="<?php echo esc_url( $edit_url ); ?>"><?php echo esc_html( $p->post_title ); ?></a>
						</td>
						<td style="font-size:12px;"><?php echo esc_html( $product->get_sku() ?: '--' ); ?></td>
						<td style="font-size:12px;">
							<a href="https://www.ebay.com/itm/<?php echo esc_attr( $item_id ); ?>" target="_blank" rel="noopener">
								<?php echo esc_html( $item_id ); ?>
							</a>
						</td>
						<td>
							<span style="display:inline-block;padding:2px 8px;border-radius:3px;font-size:11px;font-weight:600;color:#fff;background:<?php echo esc_attr( $color ); ?>;">
								<?php echo esc_html( strtoupper( $label ) ); ?>
							</span>
						</td>
						<td>
							<a href="<?php echo esc_url( $edit_url ); ?>" class="button button-small"><?php esc_html_e( 'Edit', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></a>
						</td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>

	<?php endif; // connected ?>
</div>
