<?php
/**
 * Amazon Shipment Meta Box View
 *
 * Renders the shipment confirmation meta box on order edit screen.
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/views/src/common/pages/orders
 * @author     miniOrange
 */

declare(strict_types=1);

namespace MOMCS_Sync\Views\Src\Common\Pages\Orders;

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

/**
 * Class for rendering Amazon shipment confirmation meta box.
 */
class Amazon_Shipment_Metabox_View {

	/**
	 * Render the meta box.
	 *
	 * @param int $order_id WooCommerce order ID.
	 * @return void
	 */
	public function render( int $order_id ): void {
		$order = wc_get_order( $order_id );
		if ( ! $order ) {
			echo '<p>' . esc_html__( 'Order not found.', 'multi-channel-for-woocommerce' ) . '</p>';
			return;
		}

		// Get Amazon order data.
		$amazon_order_id = $order->get_meta( '_amazon_order_id' );
		$amazon_items    = $order->get_meta( '_amazon_order_items' );
		$channel_uuid    = $order->get_meta( '_amazon_channel_uuid' );

		// Get existing shipments.
		$existing_shipments = $order->get_meta( '_amazon_shipments' );
		if ( ! is_array( $existing_shipments ) ) {
			$existing_shipments = array();
		}

		// Get current datetime for default ship date.
		$current_datetime = current_time( 'Y-m-d\TH:i' );

		?>
		<div class="momcs-shipment-metabox">
			<?php if ( empty( $channel_uuid ) ) : ?>
				<div class="notice notice-warning inline">
					<p>
						<?php
						esc_html_e(
							'This order does not have a channel UUID. It may have been imported before the shipment feature was added. Please resync the order or contact support.',
							'multi-channel-for-woocommerce'
						);
						?>
					</p>
				</div>
			<?php endif; ?>

			<div class="momcs-shipment-info">
				<p>
					<strong><?php esc_html_e( 'Amazon Order ID:', 'multi-channel-for-woocommerce' ); ?></strong>
					<?php echo esc_html( $amazon_order_id ); ?>
				</p>
			</div>

			<?php if ( ! empty( $existing_shipments ) ) : ?>
				<div class="momcs-existing-shipments">
					<h4><?php esc_html_e( 'Previously Confirmed Shipments', 'multi-channel-for-woocommerce' ); ?></h4>
					<table class="widefat">
						<thead>
							<tr>
								<th><?php esc_html_e( 'Package #', 'multi-channel-for-woocommerce' ); ?></th>
								<th><?php esc_html_e( 'Reference ID', 'multi-channel-for-woocommerce' ); ?></th>
								<th><?php esc_html_e( 'Carrier', 'multi-channel-for-woocommerce' ); ?></th>
								<th><?php esc_html_e( 'Tracking Number', 'multi-channel-for-woocommerce' ); ?></th>
								<th><?php esc_html_e( 'Ship Date', 'multi-channel-for-woocommerce' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $existing_shipments as $shipment ) : ?>
								<tr>
									<td><?php echo esc_html( $shipment['package_number'] ?? '-' ); ?></td>
									<td><?php echo esc_html( $shipment['package_reference'] ?? '-' ); ?></td>
									<td><?php echo esc_html( ( $shipment['carrier_name'] ?? '-' ) . ' (' . ( $shipment['carrier_code'] ?? '-' ) . ')' ); ?></td>
									<td><?php echo esc_html( $shipment['tracking_number'] ?? '-' ); ?></td>
									<td><?php echo esc_html( $shipment['ship_date'] ?? '-' ); ?></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				</div>
			<?php endif; ?>

			<div class="momcs-shipment-form">
				<h4><?php esc_html_e( 'Confirm New Shipment', 'multi-channel-for-woocommerce' ); ?></h4>

				<div class="momcs-packages-container">
					<!-- Packages will be dynamically added via JavaScript -->
				</div>

				<p>
					<button type="button" class="button momcs-add-package-btn">
						<?php esc_html_e( '+ Add Package', 'multi-channel-for-woocommerce' ); ?>
					</button>
				</p>

				<div class="momcs-shipment-actions">
					<button type="button" class="button button-primary momcs-confirm-shipment-btn" <?php disabled( empty( $channel_uuid ) ); ?>>
						<?php esc_html_e( 'Sync Shipment to Amazon', 'multi-channel-for-woocommerce' ); ?>
					</button>
					<span class="spinner"></span>
				</div>

				<div class="momcs-shipment-message" style="display:none;"></div>
			</div>

			<!-- Package template (hidden) -->
			<script type="text/html" id="momcs-package-template">
				<div class="momcs-package" data-package-index="{{index}}">
					<div class="momcs-package-header">
						<h4><?php esc_html_e( 'Package', 'multi-channel-for-woocommerce' ); ?> <span class="package-number">{{number}}</span></h4>
						<button type="button" class="button momcs-remove-package-btn" title="<?php esc_attr_e( 'Remove Package', 'multi-channel-for-woocommerce' ); ?>">
							<span class="dashicons dashicons-no-alt"></span>
						</button>
					</div>

					<div class="momcs-package-fields">
						<div class="momcs-field-row">
							<div class="momcs-field">
								<label>
									<?php esc_html_e( 'Package Reference ID', 'multi-channel-for-woocommerce' ); ?>
									<span class="required">*</span>
								</label>
								<input type="text" class="package-reference-id" placeholder="<?php esc_attr_e( 'e.g., pkg-001', 'multi-channel-for-woocommerce' ); ?>" value="pkg-{{number}}" required />
							</div>

							<div class="momcs-field">
								<label>
									<?php esc_html_e( 'Carrier Code', 'multi-channel-for-woocommerce' ); ?>
									<span class="required">*</span>
								</label>
								<input type="text" class="carrier-code" placeholder="<?php esc_attr_e( 'e.g., UPS', 'multi-channel-for-woocommerce' ); ?>" required />
							</div>
						</div>

						<div class="momcs-field-row">
							<div class="momcs-field">
								<label>
									<?php esc_html_e( 'Carrier Name', 'multi-channel-for-woocommerce' ); ?>
									<span class="required">*</span>
								</label>
								<input type="text" class="carrier-name" placeholder="<?php esc_attr_e( 'e.g., United Parcel Service', 'multi-channel-for-woocommerce' ); ?>" required />
							</div>

							<div class="momcs-field">
								<label>
									<?php esc_html_e( 'Shipping Method', 'multi-channel-for-woocommerce' ); ?>
									<span class="required">*</span>
								</label>
								<input type="text" class="shipping-method" placeholder="<?php esc_attr_e( 'e.g., STANDARD', 'multi-channel-for-woocommerce' ); ?>" required />
							</div>
						</div>

						<div class="momcs-field-row">
							<div class="momcs-field">
								<label>
									<?php esc_html_e( 'Tracking Number', 'multi-channel-for-woocommerce' ); ?>
									<span class="required">*</span>
								</label>
								<input type="text" class="tracking-number" placeholder="<?php esc_attr_e( 'Enter tracking number', 'multi-channel-for-woocommerce' ); ?>" required />
							</div>

							<div class="momcs-field">
								<label>
									<?php esc_html_e( 'Ship Date', 'multi-channel-for-woocommerce' ); ?>
									<span class="required">*</span>
								</label>
								<input type="datetime-local" class="ship-date" value="<?php echo esc_attr( $current_datetime ); ?>" required />
							</div>
						</div>

						<div class="momcs-field">
							<label>
								<?php esc_html_e( 'Select Items for this Package', 'multi-channel-for-woocommerce' ); ?>
								<span class="required">*</span>
							</label>
							<div class="momcs-order-items">
								<?php if ( ! empty( $amazon_items ) && is_array( $amazon_items ) ) : ?>
									<?php foreach ( $amazon_items as $item ) : ?>
										<div class="momcs-order-item">
											<label>
												<input type="checkbox" class="item-checkbox" data-order-item-id="<?php echo esc_attr( $item['OrderItemId'] ?? '' ); ?>" />
												<span class="item-title"><?php echo esc_html( $item['Title'] ?? $item['SellerSKU'] ?? 'Unknown Item' ); ?></span>
												<span class="item-quantity">
													<?php
													echo esc_html(
														sprintf(
															/* translators: %d: Item quantity */
															__( 'Qty: %d', 'multi-channel-for-woocommerce' ),
															$item['QuantityOrdered'] ?? 1
														)
													);
													?>
												</span>
												<input type="number" class="item-quantity-input" min="1" max="<?php echo esc_attr( $item['QuantityOrdered'] ?? 1 ); ?>" value="<?php echo esc_attr( $item['QuantityOrdered'] ?? 1 ); ?>" style="display:none;" />
											</label>
										</div>
									<?php endforeach; ?>
								<?php else : ?>
									<p><?php esc_html_e( 'No items found for this order.', 'multi-channel-for-woocommerce' ); ?></p>
								<?php endif; ?>
							</div>
						</div>
					</div>
				</div>
			</script>

			<!-- Hidden data -->
			<input type="hidden" id="momcs-order-id" value="<?php echo esc_attr( $order_id ); ?>" />
			<input type="hidden" id="momcs-shipment-nonce" value="<?php echo esc_attr( wp_create_nonce( 'momcs_shipment_confirmation_nonce' ) ); ?>" />
		</div>
		<?php
	}
}

