<?php
/**
 * Manage Tab View
 *
 * Shows a tabular list of connected channels with basic actions (edit/delete)
 * as previously rendered by Dashboard_Controller::render_manage_tab().
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/views/src/common/pages/dashboard
 */

declare(strict_types=1);

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

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class Manage_Tab_View
 */
class Manage_Tab_View {
	/**
	 * List of channels returned by Dashboard_Handler.
	 *
	 * @var array<int, array<string, mixed>>
	 */
	private array $channels;

	/**
	 * Constructor.
	 *
	 * @param array<int, array<string, mixed>> $channels Channel rows.
	 */
	public function __construct( array $channels ) {
		$this->channels = $channels;
	}

	/**
	 * Render the manage tab.
	 *
	 * @return void
	 */
	public function render(): void {
		?>
		<div class="momcs-sync-manage">
			<h2><?php esc_html_e( 'Manage Marketplaces', 'multi-channel-for-woocommerce' ); ?></h2>

			<?php if ( empty( $this->channels ) ) : ?>
				<div class="momcs-sync-empty-state">
					<p><?php esc_html_e( 'No marketplaces connected yet.', 'multi-channel-for-woocommerce' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=momcs-sync-dashboard&tab=connect' ) ); ?>" class="momcs-connect-btn">
						<?php esc_html_e( 'Connect a Marketplace', 'multi-channel-for-woocommerce' ); ?>
					</a>
				</div>
			<?php else : ?>
				<table class="wp-list-table widefat fixed striped">
					<thead>
					<tr>
						<th><?php esc_html_e( 'Connection Name', 'multi-channel-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Marketplace', 'multi-channel-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Region', 'multi-channel-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Status', 'multi-channel-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Last Sync', 'multi-channel-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Actions', 'multi-channel-for-woocommerce' ); ?></th>
					</tr>
					</thead>
					<tbody>
					<?php foreach ( $this->channels as $channel ) : ?>
						<tr>
							<td><?php echo esc_html( $channel['connection_name'] ); ?></td>
							<td><?php echo esc_html( ucfirst( $channel['marketplace'] ) ); ?></td>
							<?php
							$region_raw   = isset( $channel['region'] ) ? (string) $channel['region'] : '';
							$region_upper = strtoupper( trim( $region_raw ) );
							$country_name = \MOMCS_Sync\Includes\Common\Utils\Marketplace_IDs::get_country_name_by_marketplace_id( $region_upper );
							$country_disp = $country_name ?? strtoupper( $this->map_region_to_country( $region_raw ) );
							?>
							<td><?php echo esc_html( $country_disp ); ?></td>
							<td>
								<?php if ( 'active' === $channel['status'] ) : ?>
									<span class="momcs-sync-status-connected">
										<span class="dashicons dashicons-yes-alt"></span>
										<?php esc_html_e( 'Connected', 'multi-channel-for-woocommerce' ); ?>
									</span>
								<?php else : ?>
									<span class="momcs-sync-status-disconnected">
										<span class="dashicons dashicons-no-alt"></span>
										<?php esc_html_e( 'Disconnected', 'multi-channel-for-woocommerce' ); ?>
									</span>
								<?php endif; ?>
							</td>
							<td>
								<?php
								if ( ! empty( $channel['last_sync'] ) ) {
									echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $channel['last_sync'] ) ) );
								} else {
									esc_html_e( 'Never', 'multi-channel-for-woocommerce' );
								}
								?>
							</td>
							<td>
								<div class="momcs-sync-actions">
									<a href="#" class="button danger momcs-sync-delete-channel" data-id="<?php echo esc_attr( $channel['id'] ); ?>">
										<span class="dashicons dashicons-trash" style="margin-top: 3px;"></span>
										<?php esc_html_e( 'Delete', 'multi-channel-for-woocommerce' ); ?>
									</a>
								</div>
							</td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Map SP-API region codes to ISO-3166 country codes.
	 *
	 * @param string $region Region code.
	 * @return string
	 */
	private function map_region_to_country( string $region ): string {
		$mapping = array(
			'atvpdkikx0der'  => 'US',
			'a2euq1wtgctbg2' => 'CA',
			'a1pa6795ukmfr9' => 'DE',
			'a1rkkupihcs9hs' => 'ES',
			'a13vqmvgh1j6vh' => 'FR',
			'apaybkwhqasin'  => 'IT',
			'a21tjrujahjze'  => 'IN',
			'a1f83g8c2aro7p' => 'UK',
			'a1vc38t7yxb528' => 'JP',
			'a39ibj37trp1c6' => 'AU',
		);

		$key = strtolower( trim( $region ) );
		return $mapping[ $key ] ?? $region;
	}
}
