<?php
/**
 * Bulk price adjustment UI for the sbuy_product list table.
 *
 * Registers a toolbar button + hidden modal on Products list, plus the admin
 * script that drives the Preview → Apply flow (AJAX handlers live in
 * SBUY_Ajax).
 *
 * @package SimplyBuy
 */

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

add_action( 'manage_posts_extra_tablenav', 'sbuy_bulk_price_toolbar', 10, 1 );
add_action( 'admin_enqueue_scripts', 'sbuy_bulk_price_enqueue' );
add_action( 'admin_menu', 'sbuy_register_price_adjustment_page', 25 );

function sbuy_register_price_adjustment_page(): void {
	add_submenu_page(
		'simply-buy',
		esc_html__( 'Price Adjustment', 'simply-buy' ),
		esc_html__( 'Price Adjustment', 'simply-buy' ),
		'manage_options',
		'sbuy-price-adjustment',
		'sbuy_render_price_adjustment_page'
	);
}

function sbuy_render_price_adjustment_page(): void {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}

	if ( ! sbuy_has_pro_access() ) {
		echo '<div class="wrap">';
		echo '<h1>' . esc_html__( 'Price Adjustment', 'simply-buy' ) . '</h1>';
		echo '<div style="background:#f0f7ff; border-left:4px solid #2271b1; padding:20px; max-width:500px;">';
		echo '<h3>⭐ ' . esc_html__( 'Pro feature', 'simply-buy' ) . '</h3>';
		echo '<p>' . esc_html__( 'Bulk price adjustment is available in SimplyBuy Pro.', 'simply-buy' ) . '</p>';
		echo '<a href="' . esc_url( __( 'https://profitclicker.com/pricing/', 'simply-buy' ) ) . '" target="_blank" rel="noopener noreferrer" class="button button-primary">';
		echo esc_html__( 'Upgrade to Pro', 'simply-buy' );
		echo '</a>';
		echo '</div>';
		echo '</div>';
		return;
	}

	// Reuse the same IDs/behaviour as the toolbar modal — script is enqueued below.
	?>
	<div class="wrap">
		<h1><?php esc_html_e( 'Price Adjustment', 'simply-buy' ); ?></h1>
		<p><?php esc_html_e( 'Increase or decrease the price of every SimplyBuy product at once.', 'simply-buy' ); ?></p>

		<table class="form-table" role="presentation" style="max-width:500px;">
			<tr>
				<th scope="row"><label for="sbuy-price-direction"><?php esc_html_e( 'Direction:', 'simply-buy' ); ?></label></th>
				<td>
					<select id="sbuy-price-direction">
						<option value="increase"><?php esc_html_e( 'Increase', 'simply-buy' ); ?></option>
						<option value="decrease"><?php esc_html_e( 'Decrease', 'simply-buy' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Adjust by:', 'simply-buy' ); ?></th>
				<td>
					<label style="display:block; margin-bottom:6px;">
						<input type="radio" name="sbuy-price-mode" value="percent" checked>
						<?php esc_html_e( 'Percentage (%):', 'simply-buy' ); ?>
						<input type="number" id="sbuy-price-percent" min="0.1" max="100" step="0.1" style="width:120px;">
					</label>
					<label style="display:block;">
						<input type="radio" name="sbuy-price-mode" value="fixed">
						<?php esc_html_e( 'Or fixed amount:', 'simply-buy' ); ?>
						<input type="number" id="sbuy-price-amount" min="0.01" step="0.01" style="width:120px;" disabled>
						<span><?php echo esc_html( defined( 'SBUY_CORE_CURRENCY_SYMBOL' ) ? SBUY_CORE_CURRENCY_SYMBOL : '€' ); ?></span>
					</label>
				</td>
			</tr>
		</table>

		<div id="sbuy-price-preview" style="background:#f0f7ff; padding:12px; border-radius:4px; margin:16px 0; max-width:500px; display:none;"></div>

		<p>
			<button type="button" id="sbuy-price-preview-btn" class="button"><?php esc_html_e( 'Preview', 'simply-buy' ); ?></button>
			<button type="button" id="sbuy-price-apply-btn" class="button button-primary" style="margin-left:8px;" disabled><?php esc_html_e( 'Apply to all products', 'simply-buy' ); ?></button>
		</p>

		<p style="color:#d63638; font-size:12px;">⚠️ <?php esc_html_e( 'This action cannot be undone.', 'simply-buy' ); ?></p>

		<!-- Hidden elements the shared script expects on the products list -->
		<div id="sbuy-bulk-price-modal" style="display:none;"></div>
	</div>
	<?php
}

function sbuy_bulk_price_toolbar( string $which ): void {
	$screen = get_current_screen();
	if ( ! $screen || 'sbuy_product' !== $screen->post_type || 'top' !== $which ) {
		return;
	}

	if ( ! sbuy_has_pro_access() ) {
		?>
		<button type="button" class="button" disabled style="opacity:0.5; margin-left:8px;" title="<?php esc_attr_e( 'Pro feature — upgrade to unlock', 'simply-buy' ); ?>">
			<?php esc_html_e( 'Adjust all prices', 'simply-buy' ); ?> ↑↓ ⭐
		</button>
		<a href="<?php echo esc_url( __( 'https://profitclicker.com/pricing/', 'simply-buy' ) ); ?>" target="_blank" rel="noopener noreferrer" style="margin-left:6px; font-size:12px;"><?php esc_html_e( 'Upgrade to Pro', 'simply-buy' ); ?></a>
		<?php
		return;
	}
	?>
	<button type="button" id="sbuy-bulk-price-btn" class="button" style="margin-left:8px;">
		<?php esc_html_e( 'Adjust all prices', 'simply-buy' ); ?> ↑↓
	</button>

	<div id="sbuy-bulk-price-modal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.5); z-index:9999;">
		<div style="background:#fff; width:420px; margin:100px auto; padding:30px; border-radius:8px;">
			<h2><?php esc_html_e( 'Adjust all prices', 'simply-buy' ); ?></h2>

			<p>
				<label><?php esc_html_e( 'Direction:', 'simply-buy' ); ?></label><br>
				<select id="sbuy-price-direction">
					<option value="increase"><?php esc_html_e( 'Increase', 'simply-buy' ); ?></option>
					<option value="decrease"><?php esc_html_e( 'Decrease', 'simply-buy' ); ?></option>
				</select>
			</p>

			<p>
				<label style="display:block; margin-bottom:6px;">
					<input type="radio" name="sbuy-price-mode" value="percent" checked>
					<?php esc_html_e( 'Percentage (%):', 'simply-buy' ); ?>
					<input type="number" id="sbuy-price-percent" min="0.1" max="100" step="0.1" style="width:100px;">
				</label>
				<label style="display:block;">
					<input type="radio" name="sbuy-price-mode" value="fixed">
					<?php esc_html_e( 'Or fixed amount:', 'simply-buy' ); ?>
					<input type="number" id="sbuy-price-amount" min="0.01" step="0.01" style="width:100px;" disabled>
					<span><?php echo esc_html( defined( 'SBUY_CORE_CURRENCY_SYMBOL' ) ? SBUY_CORE_CURRENCY_SYMBOL : '€' ); ?></span>
				</label>
			</p>

			<div id="sbuy-price-preview" style="background:#f0f7ff; padding:12px; border-radius:4px; margin:16px 0; display:none;"></div>

			<p>
				<button type="button" id="sbuy-price-preview-btn" class="button">
					<?php esc_html_e( 'Preview', 'simply-buy' ); ?>
				</button>
				<button type="button" id="sbuy-price-apply-btn" class="button button-primary" style="margin-left:8px;" disabled>
					<?php esc_html_e( 'Apply to all products', 'simply-buy' ); ?>
				</button>
				<button type="button" id="sbuy-price-cancel-btn" class="button button-secondary" style="margin-left:8px;">
					<?php esc_html_e( 'Cancel', 'simply-buy' ); ?>
				</button>
			</p>

			<p style="color:#d63638; font-size:12px;">
				⚠️ <?php esc_html_e( 'This action cannot be undone.', 'simply-buy' ); ?>
			</p>
		</div>
	</div>
	<?php
}

function sbuy_bulk_price_enqueue( string $hook ): void {
	$screen = get_current_screen();
	$is_products_list     = ( 'edit.php' === $hook && $screen && 'sbuy_product' === $screen->post_type );
	$is_adjustment_page   = ( $screen && in_array( $screen->id, array( 'simplybuy_page_sbuy-price-adjustment', 'admin_page_sbuy-price-adjustment' ), true ) );
	if ( ! $is_products_list && ! $is_adjustment_page ) {
		return;
	}
	if ( ! sbuy_has_pro_access() ) {
		return;
	}

	wp_enqueue_script(
		'sbuy-bulk-price',
		SBUY_CORE_PLUGIN_URL . 'assets/script/simplybuy-bulk-price.min.js',
		array( 'jquery' ),
		SBUY_PLUGIN_VERSION,
		true
	);
	wp_localize_script( 'sbuy-bulk-price', 'sbuy_bulk_price', array(
		'nonce'            => wp_create_nonce( 'simplybuy-nonce' ),
		'ajaxUrl'          => admin_url( 'admin-ajax.php' ),
		'i18n'             => array(
			'willAdjust'    => esc_html__( 'products will be adjusted', 'simply-buy' ),
			'examples'      => esc_html__( 'Examples:', 'simply-buy' ),
			'cannotUndo'    => esc_html__( 'This action cannot be undone.', 'simply-buy' ),
			'confirmApply'  => esc_html__( 'Apply the price change to all products? This cannot be undone.', 'simply-buy' ),
			'requestFailed' => esc_html__( 'Request failed.', 'simply-buy' ),
		),
		'currency'         => defined( 'SBUY_CORE_CURRENCY_SYMBOL' ) ? SBUY_CORE_CURRENCY_SYMBOL : '€',
	) );
}
