<?php
/**
 * Concrete implementation of Backup_Sync_Handler for backup sync operations.
 *
 * @package    ProductSync
 * @subpackage SyncHandler
 * @author     miniOrange <info@miniorange.com>
 * @link       https://miniorange.com
 */

namespace MOWCPS\Common\Handlers\SyncHandler;

use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Models\Sync_Details_DAO;

/**
 * Concrete Backup_Sync_Handler class for backup sync operations.
 */
class Backup_Sync_Handler extends Abstract_Sync_Handler {

	/**
	 * Handler type for the sync process.
	 *
	 * @var string
	 */
	public $handler_type;

	/**
	 * Sync type for the sync process.
	 *
	 * @var string
	 */
	public $sync_type;

	/**
	 * Cron arguments for the sync process.
	 *
	 * @var array
	 */
	public $cron_args;

	/**
	 * Constructor.
	 *
	 * @param array $cron_args Arguments used to initialize the sync handler.
	 */
	public function __construct( $cron_args ) {
		$connection_id = array_key_first( $cron_args['priority_queue'] );
		parent::__construct( $cron_args['supplier_id'], $connection_id );
		$this->handler_type = $cron_args['priority_queue'][ $connection_id ]['handler_type'];
		$this->cron_args    = $cron_args;
	}

	/**
	 * Start backup sync.
	 */
	public function start_backup_sync() {
		if ( Constants::INTERDEPENDENT === $this->handler_type ) {
			$handler = new Interdependant_Sync_Handler( $this->cron_args['supplier_id'], array_key_first( $this->cron_args['priority_queue'] ), $this->cron_args['schedule_id'] );
			Sync_Details_DAO::get_instance()->set_priority_queue( $this->cron_args['priority_queue'] );
			Sync_Details_DAO::get_instance()->set_schedule_id( $this->cron_args['schedule_id'] );
			Sync_Details_DAO::get_instance()->set_supplier_app_type( $this->cron_args['vendor_name'] );
			$end_status = $handler->check_sync_process( $this->cron_args );
			$this->end_sync( $end_status );
		} elseif ( Constants::INDEPENDENT === $this->handler_type ) {
			// TODO : ADD HOOK FOR INDEPENDENT SYNC FLOW.
		}
	}
}
