<?php
/**
 * Field Mapping Controller
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/includes/common/controllers/template-mapping
 * @author     miniOrange
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common\Controllers\Template_Mapping;

use MOMCS_Sync\Includes\Common\Utils\Utils;

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

/**
 * Controller class for handling field mapping functionality.
 */
class Field_Mapping_Controller {

	/**
	 * Handle saving the field mapping.
	 *
	 * @return void
	 */
	public static function handle_save_field_mapping(): void {
		// Capability or nonce verification.
		if ( ! current_user_can( 'manage_options' ) || ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'momcs_sync_save_field_mapping_nonce' ) ) {
			wp_send_json_error( array( 'message' => 'Security check failed.' ) );
			exit;
		}

		$marketplace  = isset( $_POST['marketplace'] ) ? sanitize_text_field( wp_unslash( $_POST['marketplace'] ) ) : '';
		$channel_id   = isset( $_POST['channel_id'] ) ? sanitize_text_field( wp_unslash( $_POST['channel_id'] ) ) : '';
		$product_type = isset( $_POST['product_type'] ) ? sanitize_text_field( wp_unslash( $_POST['product_type'] ) ) : '';
		$mapping_data = isset( $_POST['mapping_data'] ) ? sanitize_text_field( wp_unslash( $_POST['mapping_data'] ) ) : '';

		if ( empty( $channel_id ) ) {
			wp_send_json_error( array( 'message' => 'Channel ID is required.' ) );
			exit;
		}

		try {
			( new \MOMCS_Sync\Includes\Common\Handlers\Mapping_Handler() )
				->save_field_mapping( $marketplace, $mapping_data, $product_type, $channel_id );
			wp_send_json_success( array( 'message' => 'Field mapping saved successfully.' ) );
		} catch ( \Throwable $e ) {
			wp_send_json_error( array( 'message' => $e->getMessage() ) );
		}
	}

	/**
	 * Handle getting the field mapping.
	 *
	 * @return void
	 */
	public static function handle_get_field_mapping(): void {
		// Capability or nonce verification.
		if ( ! current_user_can( 'manage_options' ) || ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'momcs_sync_get_field_mapping_nonce' ) ) {
			wp_send_json_error( array( 'message' => 'Security check failed.' ) );
			exit;
		}

		// Get marketplace.
		$marketplace = isset( $_POST['marketplace'] ) ? sanitize_text_field( wp_unslash( $_POST['marketplace'] ) ) : '';
		$channel_id  = isset( $_POST['channel_id'] ) ? sanitize_text_field( wp_unslash( $_POST['channel_id'] ) ) : '';

		// Get product type.
		$product_type = isset( $_POST['product_type'] ) ? sanitize_text_field( wp_unslash( $_POST['product_type'] ) ) : '';

		if ( empty( $channel_id ) ) {
			wp_send_json_error( array( 'message' => 'Channel ID is required.' ) );
			exit;
		}

		try {
			$handler = new \MOMCS_Sync\Includes\Common\Handlers\Mapping_Handler();
			$data    = $handler->get_field_mapping( $marketplace, $channel_id );
		} catch ( \Throwable $e ) {
			wp_send_json_error( array( 'message' => $e->getMessage() ) );
			exit;
		}

		if ( isset( $data['selected_category'] ) && $data['selected_category'] === $product_type ) {
			wp_send_json_success( array( 'mappings' => $data['field_mappings'] ?? array() ) );
		} else {
			wp_send_json_success( array( 'mappings' => array() ) );
		}
	}
}
