<?php
/**
 * Marketplace View Factory Class
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/views/src/common/pages/supplier/apps
 * @author     miniOrange
 */

declare(strict_types=1);

namespace MOMCS_Sync\Views\Src\Common\Pages\Supplier\Apps;

use MOMCS_Sync\Includes\Common\Constants\Plugin_Constants;

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

use MOMCS_Sync\Views\Src\Common\Pages\Supplier\Authorization\Amazon_Authorization_View;
use MOMCS_Sync\Views\Src\Common\Pages\Supplier\Attribute_Mapping\Amazon_Mapping_View;
use MOMCS_Sync\Includes\Common\Utils\Utils;
use MOMCS_Sync\Views\Src\Common\Pages\Supplier\Sync_Settings\Amazon_Sync_Settings_View;
use MOMCS_Sync\Views\Src\Common\Pages\Supplier\Apps\Marketplace_Tab_View;

/**
 * Factory class for creating marketplace-specific views
 */
class Marketplace_View_Factory {
	/**
	 * Create a marketplace view for the specified marketplace.
	 *
	 * @param string   $marketplace   The marketplace to create a view for (amazon, ebay, walmart).
	 * @param int|null $current_step  Optional current step override.
	 *
	 * @return object|null The marketplace-specific view or null if invalid.
	 */
	public static function create_marketplace_view( $marketplace, $current_step = null ) {
		// Use provided step or fallback to GET parameter (cast to int) or default to 1.
		if ( null === $current_step ) {
			$current_step_str = Utils::get_current_page_parameter_value( 'step', '1' );
			$current_step     = is_numeric( $current_step_str ) ? (int) $current_step_str : 1;
		}

		// Create the stepper view with appropriate components (plan-aware 3/4-step flow).
		try {
			$authorization_view  = self::create_authorization_view( $marketplace );
			$mapping_view        = self::create_mapping_view( $marketplace );
			$sync_settings_view  = self::create_sync_settings_view( $marketplace );

			return new Marketplace_Stepper_View(
				$authorization_view,
				$mapping_view,
				$sync_settings_view,
				ucfirst( $marketplace ),
				$current_step
			);
		} catch ( \Exception $e ) {
			return null;
		}
	}

	/**
	 * Create a tab-based marketplace view (2-tab layout) for the specified marketplace.
	 *
	 * @param string $marketplace    The marketplace slug (e.g. 'amazon').
	 * @param string $active_tab     Active tab ('authorization' or 'sync').
	 * @param bool   $is_authorized  Whether the channel is authorized.
	 * @param string $channel_uuid   Channel UUID.
	 *
	 * @return Marketplace_Tab_View|null The tab view or null if invalid.
	 */
	public static function create_marketplace_tab_view(
		string $marketplace,
		string $active_tab,
		bool $is_authorized,
		string $channel_uuid
	): ?Marketplace_Tab_View {
		try {
			$authorization_view = self::create_authorization_view( $marketplace );
			$mapping_view       = self::create_mapping_view( $marketplace );
			$sync_settings_view = self::create_sync_settings_view( $marketplace );

			return new Marketplace_Tab_View(
				$authorization_view,
				$mapping_view,
				$sync_settings_view,
				ucfirst( $marketplace ),
				$active_tab,
				$is_authorized,
				$channel_uuid
			);
		} catch ( \Exception $e ) {
			return null;
		}
	}

	/**
	 * Create an authorization view for the specified marketplace.
	 *
	 * @param string $marketplace The marketplace to create a view for (amazon, ebay, walmart).
	 *
	 * @return object The marketplace-specific authorization view.
	 * @throws \InvalidArgumentException If the marketplace is unknown.
	 */
	public static function create_authorization_view( $marketplace ) {
		switch ( $marketplace ) {
			case Plugin_Constants::AMAZON:
				return new Amazon_Authorization_View();
			default:
				throw new \InvalidArgumentException( esc_html( "Unknown marketplace: {$marketplace}" ) );
		}
	}

	/**
	 * Create a field mapping view for the specified marketplace.
	 *
	 * @param string $marketplace The marketplace to create a view for (amazon, ebay, walmart).
	 *
	 * @return object The marketplace-specific field mapping view.
	 * @throws \InvalidArgumentException If the marketplace is unknown.
	 */
	public static function create_mapping_view( $marketplace ) {
		switch ( $marketplace ) {
			case Plugin_Constants::AMAZON:
				return new Amazon_Mapping_View();
			default:
				throw new \InvalidArgumentException( esc_html( "Unknown marketplace: {$marketplace}" ) );
		}
	}

	/**
	 * Create a sync settings view for the specified marketplace.
	 *
	 * @param string $marketplace The marketplace to create a view for (amazon, ebay, walmart).
	 * @return object The marketplace-specific sync settings view.
	 * @throws \InvalidArgumentException If the marketplace is unknown.
	 */
	public static function create_sync_settings_view( $marketplace ) {
		switch ( $marketplace ) {
			case Plugin_Constants::AMAZON:
				return new Amazon_Sync_Settings_View();
			default:
				throw new \InvalidArgumentException( esc_html( "Unknown marketplace: {$marketplace}" ) );
		}
	}
}
