<?php
/**
 * Sanmar App
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Suppliers
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Suppliers;

use WP_Error;
use MOWCPS\Common\Abstracts\Base_Supplier_App;
use MOWCPS\Common\Interfaces\Supplier_App_Interface;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Services\FTP\FTP_Service;
use MOWCPS\Common\Services\Logging\Supplier_Logger;

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

/**
 * Class Sanmar
 *
 * Handles Sanmar supplier app operations
 */
class Sanmar extends Base_Supplier_App implements Supplier_App_Interface {
	/**
	 * Constructor
	 */
	public function __construct() {
		parent::__construct( Constants::SUPPLIER_APP_TYPE_SANMAR );
	}

	/**
	 * Perform Sanmar-specific connection test
	 *
	 * @param array $supplier_data Supplier data.
	 * @return array Test result.
	 */
	public function perform_connection_test( $supplier_data ) {
		$validation_result = $this->validate_supplier_specific_configuration( $supplier_data );
		if ( is_wp_error( $validation_result ) ) {
			return $this->get_test_result(
				false,
				sprintf( Constants::SUPPLIER_FTP_CONNECTION_FAILED, $this->supplier_name, $validation_result->get_error_message() ),
			);
		}

		// Load and process configuration with dynamic parameters.
		$processed_config = $this->load_and_process_configuration( $supplier_data );
		if ( is_wp_error( $processed_config ) ) {
			return $this->get_test_result(
				false,
				sprintf( Constants::SUPPLIER_CONFIG_LOADING_FAILED, $this->supplier_name, $processed_config->get_error_message() ),
			);
		}

		$connections     = $processed_config['connections'] ?? array();
		$test_connection = $this->find_test_connection( $connections );

		if ( empty( $test_connection ) ) {
			return $this->get_test_result(
				false,
				sprintf( Constants::SUPPLIER_NO_TEST_CONNECTION, $this->supplier_name ),
			);
		}

		$config   = $test_connection['configuration'];
		$host     = $config['host'];
		$port     = (int) $config['port'];
		$username = $config['username'];
		$password = $config['password'];

		if ( empty( $host ) ) {
			return $this->get_test_result(
				false,
				sprintf( Constants::SUPPLIER_NO_FTP_HOST, $this->supplier_name ),
			);
		}

		$connection_result = FTP_Service::connect_and_login_ftp( $host, $username, $password, 10, $port );

		if ( false === $connection_result ) {
			return $this->get_test_result(
				false,
				sprintf( Constants::SUPPLIER_FTP_CONNECTION_FAILED, $this->supplier_name, 'Failed to connect to FTP server' ),
				Constants::HTTP_UNAUTHORIZED,
			);
		}

		return $this->get_test_result(
			true,
			sprintf( Constants::SUPPLIER_AUTH_SUCCESSFUL, $this->supplier_name ),
			Constants::HTTP_OK,
		);
	}

	/**
	 * Validate Sanmar-specific configuration
	 *
	 * @param array $supplier_data Supplier data to validate.
	 * @return bool|WP_Error True if valid, WP_Error if invalid.
	 */
	public function validate_supplier_specific_configuration( $supplier_data ) {
		if ( empty( $supplier_data['username'] ) ) {
			return new WP_Error(
				Constants::ERROR_INVALID_SUPPLIER_CONFIG,
				sprintf( Constants::SUPPLIER_MISSING_USERNAME, $this->supplier_name ),
				array( 'status' => Constants::HTTP_BAD_REQUEST )
			);
		}

		if ( empty( $supplier_data['password'] ) ) {
			return new WP_Error(
				Constants::ERROR_INVALID_SUPPLIER_CONFIG,
				sprintf( Constants::SUPPLIER_MISSING_PASSWORD, $this->supplier_name ),
				array( 'status' => Constants::HTTP_BAD_REQUEST )
			);
		}

		return true;
	}
}
