<?php
/**
 * Lead Connector Migrator
 * Copyright (C) 2025 Lead Connector
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
if ( ! defined( 'ABSPATH' ) ) {
	die( 'Well Sir You Cannot Jump Here...haha' );
}

class Lcm_Start_Server_Import {

	public static function execute( $params ) {
		if ( ! isset( $params['lcm_start_server_import'] ) || $params['lcm_start_server_import'] != 1 ) {
			return $params;
		}

		Lcm_Status::info( __( 'Starting Migration', 'lc-migrator' ), Lcm_Stages::$STAGES['START_SERVER_IMPORT'] );

		$public_url = get_option( LC_MIGRATOR_PUBLIC_URL_RESTORE );
		$site_id    = $params['site_id'] ?? null;

		if ( ! $public_url || ! $site_id ) {
			throw new Lcm_Export_Exception( __( 'Unable to start server import: missing backup URL or site ID.', 'lc-migrator' ) );
		}

		$lc_admin = new \Lcmigrator\LcMigrator\Admin\LcMigrator_Admin( LC_MIGRATOR_PLUGIN_NAME, LC_MIGRATOR_VERSION );
		$lc_admin->lc_wp_get( 'process_migration', array( 'siteId' => $site_id, 'backup_url' => $public_url ) );

		Lcm_Status::info( __( 'Migration Started... Please Stay On This Page.', 'lc-migrator' ), Lcm_Stages::$STAGES['START_SERVER_IMPORT'] );

		unset( $params['lcm_start_server_import'] );
		$params['lcm_start_pool_requests'] = 1;

		return $params;
	}
}