<?php
/**
 * This file is for performing the connect external supplier handler functionality.
 *
 * @package    Common
 * @subpackage ConnectExternalSupplierHandler
 * @author     miniOrange <info@miniorange.com>
 * @link       https://miniorange.com
 */

namespace MOWCPS\Common\Handlers\ConnectExternalSupplierHandler;

use MOWCPS\Common\Models\Setting_Model;
use MOWCPS\Common\Models\Authorization_Model;
use MOWCPS\Common\Services\API\API_Service;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Models\Sync_Details_DAO;
use MOWCPS\Common\Services\Pagination\Pagination_Service;
use MOWCPS\Common\Services\Authorization\Authorization_Service;
use MOWCPS\Common\Services\FTP\FTP_Service;
use MOWCPS\Common\Services\Logging\Supplier_Logger;

/**
 * Connect External Supplier Handler.
 */
class Connect_FTP_Handler {

	/**
	 * Connection ID.
	 *
	 * @var int
	 */
	public $connection_id;

	/**
	 * Supplier ID.
	 *
	 * @var int
	 */
	public $supplier_id;

	/**
	 * Supplier details.
	 *
	 * @var array
	 */
	public $supplier_details;

	/**
	 * Connection details.
	 *
	 * @var array
	 */
	public $connection_details;

	/**
	 * Page number for the sync process.
	 *
	 * @var int
	 */
	public $page_number;

	/**
	 * Constructor.
	 *
	 * @param int $connection_id The connection ID.
	 * @param int $supplier_id   The supplier ID.
	 * @param int $page_number   The page number for the sync. Default 1.
	 */
	public function __construct( $connection_id, $supplier_id, $page_number = 1 ) {
		$this->connection_id = $connection_id;
		$this->supplier_id   = $supplier_id;
		$this->page_number   = $page_number;
	}

	/**
	 * Handles connecting to supplier via FTP and retrieving files.
	 *
	 * @return array FTP response data including success or error information.
	 */
	public function connect_ftp_handler( $connection_id, $supplier_id ) {
		Supplier_Logger::debug( $supplier_id, 'Connecting to FTP server for connection: ' . $connection_id );

		$ftp_services = new FTP_Service();
		$ftp_response = $ftp_services->get_file_from_ftp( $supplier_id, $connection_id );

		if ( isset( $ftp_response['success'] ) && $ftp_response['success'] ) {
			Supplier_Logger::debug( $supplier_id, 'Successfully retrieved file from FTP server' );
		} else {
			Supplier_Logger::debug( $supplier_id, 'Failed to retrieve file from FTP server' );
			if ( isset( $ftp_response['error'] ) ) {
				Supplier_Logger::debug( $supplier_id, 'FTP error: ' . $ftp_response['error'] );
			}
		}

		return $ftp_response;
	}

	/**
	 * Processes a file received via API and converts it into usable data.
	 *
	 * @param int    $connection_id  Connection ID.
	 * @param string $sync_filename File name to save the response as.
	 * @param string $file_in_zip   Path to file inside a ZIP archive, if applicable.
	 * @param mixed  $api_response  Raw API response data.
	 *
	 * @return array Conversion result with status and file information.
	 */
	public function file_from_api_handler( $connection_id, $sync_filename, $file_in_zip, $api_response ) {
		$ftp_services = new FTP_Service();
		$ftp_response = $ftp_services->convert_file_data( $connection_id, $sync_filename, $file_in_zip, $api_response );
		return $ftp_response;
	}
}
