<?php
/**
 * NMF Variation model.
 *
 * @package flo-sync
 *
 * @phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag
 */

namespace NMFS\Sync\Items\Articles\Models;

use Automattic\WooCommerce\Enums\ProductType;
use InvalidArgumentException;
use NMFS\Shared;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\WP_Repository;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Models\Hub_API_Properties;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Loggers\Log_Level_Enum;

defined( 'ABSPATH' ) || exit;

/**
 * Class Variation_Model
 */
class Variation_Model extends Article_Model {

	/**
	 * Constructor method for the class.
	 *
	 * @param Article_Model $article_model The article model.
	 *
	 * @throws InvalidArgumentException If the article model is not of type variation.
	 */
	public function __construct( Article_Model $article_model ) {

		if ( ! $article_model->wc_product->is_type( ProductType::VARIATION ) ) {
			Shared::instance()->logger->log(
				'sync',
				'Attempted to create Variation_Model with non-variation article model.',
				array(
					'product_id' => $article_model->wc_product->get_id(),
					'sku'        => $article_model->sku,
				),
				Log_Level_Enum::CRITICAL
			);
			throw new InvalidArgumentException( 'Article_Model must be of type variation.' );
		}

		parent::__construct(
			$article_model->publish_status,
			$article_model->sku,
			$article_model->quantity,
			$article_model->title,
			$article_model->manages_stock,
			$article_model->display_order,
			$article_model->description_html,
			$article_model->description_raw,
			$article_model->hub_category_id,
			$article_model->price,
			$article_model->article_account_code_id,
			$article_model->barcode,
			$article_model->barcode_alternatives,
			$article_model->images,
			$article_model->order_ticket_dialogue,
			$article_model->order_ticket_text,
			$article_model->info_updated_time,
			$article_model->quantity_updated_time,
			$article_model->stored_hash,
			$article_model->wc_product,
			$article_model->hub_type,
			$article_model->is_web_shop_article,
			$article_model->hub_api_data,
			$article_model->unit
		);
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_data_to_send_to_api( bool $creating_new_article ): array {

		$article_api_data = parent::get_data_to_send_to_api( $creating_new_article );

		if ( ! $creating_new_article ) {
			return $article_api_data;
		}

		// Force values for variation articles.
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_TEXT ] = '';
		$article_api_data[ Hub_API_Properties::ARTICLE_IS_QUICK_BUTTON ]   = false;

		// Default values, will be replaced from parent article, if found.
		$article_api_data[ Hub_API_Properties::ARTICLE_ACCUMULATIVE ]          = 1;
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_DIALOGUE ] = 0;
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_PRINTER ]  = 1;
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_COPIES ]   = 1;
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_TYPE ]     = 0;
		$article_api_data[ Hub_API_Properties::ARTICLE_TYPE ]                  = 0; // Article type: Goods.
		$article_api_data[ Hub_API_Properties::ARTICLE_QUICK_BUTTON_COLOR ]    = '#D7BBFA';

		$parent_id      = $this->wc_product->get_parent_id();
		$parent_product = wc_get_product( $parent_id );

		if ( empty( $parent_product?->get_sku() ) ) {
			Shared::instance()->logger->log(
				'article-sync',
				'No parent SKU found when creating variation article.',
				array(
					'product_id' => $this->wc_product->get_id(),
					'parent_id'  => $parent_id,
				),
				Log_Level_Enum::DEBUG
			);
			return $article_api_data;
		}

		$hub_article = WP_Repository::get_current_main_account()?->api->get_article( $parent_product->get_sku() );

		if ( empty( $hub_article ) ) {
			Shared::instance()->logger->log(
				'article-sync',
				'No parent article found in Hub when creating variation article.',
				array(
					'product_id'  => $this->wc_product->get_id(),
					'parent_id'   => $parent_id,
					'parent_sku'  => $parent_product->get_sku(),
					'hub_article' => $hub_article,
				),
				Log_Level_Enum::DEBUG
			);
			return $article_api_data;
		}

		$article_api_data[ Hub_API_Properties::ARTICLE_ACCUMULATIVE ]          = $hub_article[ Hub_API_Properties::ARTICLE_ACCUMULATIVE ];
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_DIALOGUE ] = $hub_article[ Hub_API_Properties::ARTICLE_ORDER_TICKET_DIALOGUE ];
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_PRINTER ]  = $hub_article[ Hub_API_Properties::ARTICLE_ORDER_TICKET_PRINTER ];
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_COPIES ]   = $hub_article[ Hub_API_Properties::ARTICLE_ORDER_TICKET_COPIES ];
		$article_api_data[ Hub_API_Properties::ARTICLE_ORDER_TICKET_TYPE ]     = $hub_article[ Hub_API_Properties::ARTICLE_ORDER_TICKET_TYPE ];
		$article_api_data[ Hub_API_Properties::ARTICLE_TYPE ]                  = $hub_article[ Hub_API_Properties::ARTICLE_TYPE ];
		$article_api_data[ Hub_API_Properties::ARTICLE_QUICK_BUTTON_COLOR ]    = $hub_article[ Hub_API_Properties::ARTICLE_QUICK_BUTTON_COLOR ];

		if ( 0 === $article_api_data[ Hub_API_Properties::ARTICLE_TYPE ] ) {
			$article_api_data[ Hub_API_Properties::ARTICLE_GOODS_TYPE ] = $hub_article[ Hub_API_Properties::ARTICLE_GOODS_TYPE ];
		}

		Shared::instance()->logger->log(
			'article-sync',
			'Copied properties from parent article in Hub for variation article.',
			array(
				'product_id'   => $this->wc_product->get_id(),
				'updated_data' => $article_api_data,
			)
		);

		return $article_api_data;
	}
}
