<?php

namespace Meliconnect\Meliconnect\Modules\Importer\Services;

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

use Meliconnect\Meliconnect\Core\Helpers\Helper;
use Meliconnect\Meliconnect\Core\Helpers\MeliconMeli;
use Meliconnect\Meliconnect\Core\Models\Template;
use Meliconnect\Meliconnect\Core\Models\UserConnection;
use Meliconnect\Meliconnect\Modules\Importer\Models\UserListingToImport;

/**
 * Orchestrates the process of obtaining transformed product data from
 * the WooCommerceProductAdapter and creating or updating products in
 * WooCommerce using WooCommerceProductCreationService.
 */

class ProductDataFacade {

	protected $wooCommerceAdapter;
	protected $productCreationService;

	public function __construct(
		WooCommerceProductAdapter $wooCommerceAdapter,
		WooCommerceProductCreationService $productCreationService
	) {
		$this->wooCommerceAdapter     = $wooCommerceAdapter;
		$this->productCreationService = $productCreationService;
	}

	public function importAndCreateProduct( $meliListingId, $meli_user_id, $template_id = null, $woo_product_id = null, $sync_options = null ) {
		$meli_user_data = UserConnection::getUser( $meli_user_id );

		if ( ! $meli_user_data ) {
			Helper::logData( 'User not found: ' . $meli_user_id, 'custom-import' );
			return false;
		}

		$meli_listing_data = MeliconMeli::getMercadoLibreListingData( $meliListingId, $meli_user_data->access_token );

        

		// Obtener datos transformados desde el servidor usando el adaptador
		$transformedData = $this->wooCommerceAdapter->getTransformedProductData( $meli_listing_data, $meli_user_id, $template_id, $woo_product_id, $sync_options );



		if ( ! isset( $transformedData['status'] ) || $transformedData['status'] !== 200 || ! isset( $transformedData['data'] ) || empty( $transformedData['data'] ) ) {
			// Manejo de errores si no se pudo obtener o transformar los datos

			Helper::logData( 'Error processing product data: ' . print_r( $transformedData, true ), 'custom-import' );
            
			return false;
		}

        //Helper::logData( 'Transformed data: ' . print_r( $transformedData, true ), 'custom-import' );

		// create woocommerce product
		$woo_products_ids = $this->productCreationService->createProduct( $transformedData['data'], $meli_listing_data['data'] );

		if ( isset( $woo_products_ids ) && ! empty( $woo_products_ids ) && is_array( $woo_products_ids ) ) {
			foreach ( $woo_products_ids as $woo_product_id ) {

				// $template_id = Template::createUpdateTemplateFromMeliListing('product', $woo_product_id, $meli_listing_data['data']);
				$template_id = get_post_meta( $woo_product_id, 'meliconnect_asoc_template_id', true );

				if ( $template_id ) {
					// Deletes and Creates template attributes
					Template::deleteCreateTemplatesAttributesFromMeliListing( $template_id, $meli_listing_data['data'], $woo_product_id );
				} else {
					Helper::logData( 'Error creating template for product: ' . $woo_product_id, 'custom-import' );
				}
			}
		}

		return true;
	}
}
