<?php
/**
 * Dashboard View Class
 *
 * @package MOMCSAmazonSync
 * @since 1.0.0
 */

declare( strict_types=1 );

namespace MOMCS\AmazonSync\Views\Dashboard;

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

use MOMCS\AmazonSync\Views\Components\Header_View;

/**
 * Render the dashboard view.
 */
class Dashboard_View {
	/**
	 * Render the dashboard view.
	 *
	 * @return void
	 */
	public function render(): void {
		// Get channels using the correct namespace and method.
		$channels       = array();
		$channels_count = 0;

		if ( class_exists( '\MOMCS_Sync\Includes\Common\Models\Multichannels_Model' ) ) {
			$model = new \MOMCS_Sync\Includes\Common\Models\Multichannels_Model();
			if ( method_exists( $model, 'get_all_channels' ) ) {
				$channels       = $model->get_all_channels();
				$channels_count = is_array( $channels ) ? count( $channels ) : 0;
			}
		}

		$plugin_url = plugin_dir_url( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) );
		?>
		<div class="wrap">
			<?php ( new Header_View( 'dashboard' ) )->render(); ?>
			
			<div class="momcs-connect-container">
				<?php if ( 0 === $channels_count ) : ?>
					<p><?php esc_html_e( 'Connect your first marketplace to start syncing products and orders.', 'multi-channel-for-woocommerce' ); ?></p>
					
					<div class="momcs-channels-wrapper-new">
						<!-- Amazon Channel -->
						<div class="momcs-channel-rect-container">
							<div class="momcs-channel-rect-content">
								<div class="momcs-channel-rect-info">
									<img src="<?php echo esc_url( $plugin_url . 'assets/images/amazon-logo.png' ); ?>" 
										alt="<?php esc_attr_e( 'Amazon Logo', 'multi-channel-for-woocommerce' ); ?>"
										class="momcs-channel-rect-logo"
									>
									<div class="momcs-channel-rect-text">
										<div class="momcs-channel-rect-name"><?php esc_html_e( 'Amazon', 'multi-channel-for-woocommerce' ); ?></div>
										<p><?php esc_html_e( 'Connect your Amazon Seller Central account to sync products.', 'multi-channel-for-woocommerce' ); ?></p>
									</div>
								</div>
								<a href="<?php echo esc_url( admin_url( 'admin.php?page=amazon-sync-dashboard&tab=connect&marketplace=amazon&step=1' ) ); ?>" class="momcs-channel-rect-connect">
									<?php esc_html_e( 'Connect', 'multi-channel-for-woocommerce' ); ?>
								</a>
							</div>
						</div>
					</div>
				<?php else : ?>
					<div class="momcs-dashboard-recent">
						<table class="widefat">
							<thead>
								<tr>
									<th><?php esc_html_e( 'Name', 'multi-channel-for-woocommerce' ); ?></th>
									<th><?php esc_html_e( 'Type', 'multi-channel-for-woocommerce' ); ?></th>
									<th><?php esc_html_e( 'Status', 'multi-channel-for-woocommerce' ); ?></th>
									<th><?php esc_html_e( 'Actions', 'multi-channel-for-woocommerce' ); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ( $channels as $channel ) : ?>
									<tr>
										<td><?php echo esc_html( $channel['connection_name'] ); ?></td>
										<td><?php echo esc_html( ucfirst( $channel['marketplace'] ) ); ?></td>
										<td>
											<span class="status-<?php echo esc_attr( $channel['status'] ); ?>">
												<?php echo esc_html( ucfirst( $channel['status'] ) ); ?>
											</span>
										</td>
										<td>
											<a href="<?php echo esc_url( admin_url( 'admin.php?page=amazon-sync-dashboard&tab=connect&action=edit&id=' . $channel['id'] ) ); ?>" class="button">
												<?php esc_html_e( 'Manage', 'multi-channel-for-woocommerce' ); ?>
											</a>
										</td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				<?php endif; ?>
			</div>
		</div>
		<?php
	}
}
