<?php
/**
 * Channel Service
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/includes/common/services
 * @author     miniOrange
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common\Services;

use MOMCS_Sync\Includes\Common\Models\Multichannels_Model;
use MOMCS_Sync\Includes\Common\Utils\Logger;
use MOMCS_Sync\Includes\Common\Constants\Plugin_Constants;

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

/**
 * Service class to encapsulate channel-related business logic
 * (formatting, icon selection, and retrieval).
 */
class Channel_Service {

	/**
	 * Channels model instance.
	 *
	 * @var Multichannels_Model
	 */
	private $multichannels_model;

	/**
	 * Logger instance.
	 *
	 * @var Logger
	 */
	private $logger;

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->multichannels_model = new Multichannels_Model();
		$this->logger              = new Logger( 'channel-service' );
	}

	/**
	 * Get raw Amazon channels from DB using the model with fallbacks.
	 *
	 * @return array<int, array<string, mixed>>
	 */
	public function get_raw_amazon_channels(): array {
		$channels = $this->multichannels_model->get_all_channels_by_marketplace( Plugin_Constants::AMAZON );
		if ( empty( $channels ) ) {
			$channels = $this->multichannels_model->get_channels_by_marketplace( Plugin_Constants::AMAZON );
		}
		return is_array( $channels ) ? $channels : array();
	}

	/**
	 * Return formatted channel payload for UI/API responses.
	 *
	 * @param array $channel Raw channel row.
	 * @return array<string, mixed>
	 */
	public function format_channel( array $channel ): array {
		$id   = isset( $channel['id'] ) ? $channel['id'] : ( $channel['uuid'] ?? '' );
		$uuid = isset( $channel['uuid'] ) ? $channel['uuid'] : $id;

		$name = 'Amazon Channel';
		if ( isset( $channel['connection_name'] ) && ! empty( $channel['connection_name'] ) ) {
			$name = (string) $channel['connection_name'];
		} elseif ( isset( $channel['name'] ) && ! empty( $channel['name'] ) ) {
			$name = (string) $channel['name'];
		}

		$marketplace_label = 'Amazon';
		if ( isset( $channel['marketplace'] ) && ! empty( $channel['marketplace'] ) ) {
			$marketplace_label = 'Amazon ' . strtoupper( (string) $channel['marketplace'] );
		} elseif ( isset( $channel['region'] ) && ! empty( $channel['region'] ) ) {
			$marketplace_label = 'Amazon ' . strtoupper( (string) $channel['region'] );
		}

		$icon = $this->get_icon_for_channel( $channel );

		return array(
			'id'          => $id,
			'uuid'        => $uuid,
			'name'        => $name,
			'marketplace' => $marketplace_label,
			'region'      => isset( $channel['region'] ) ? $channel['region'] : '',
			'icon'        => $icon,
		);
	}

	/**
	 * Get icon URL for a channel.
	 *
	 * @param array $channel Channel row.
	 * @return string Icon URL.
	 */
	public function get_icon_for_channel( array $channel ): string {
		// Default to the bundled Amazon icon.
		$default_icon = defined( 'MOMCS_PLUGIN_URL' )
			? MOMCS_PLUGIN_URL . 'assets/images/amazon-single-logo.png'
			: '';

		$type_or_marketplace = '';
		if ( isset( $channel['type'] ) && ! empty( $channel['type'] ) ) {
			$type_or_marketplace = strtolower( (string) $channel['type'] );
		} elseif ( isset( $channel['marketplace'] ) && ! empty( $channel['marketplace'] ) ) {
			$type_or_marketplace = strtolower( (string) $channel['marketplace'] );
		}

		switch ( $type_or_marketplace ) {
			case Plugin_Constants::AMAZON:
			default:
				return $default_icon;
		}
	}

	/**
	 * Get formatted channels for Amazon.
	 *
	 * @return array<int, array<string, mixed>>
	 */
	public function get_formatted_amazon_channels(): array {
		$raw = $this->get_raw_amazon_channels();
		if ( empty( $raw ) ) {
			return array();
		}
		$formatted = array();
		foreach ( $raw as $row ) {
			$formatted[] = $this->format_channel( $row );
		}
		return $formatted;
	}
}
