<?php
/**
 * All-in-One WP Migration
 * Copyright (C) 2014-2018 ServMask Inc.
 *
 * Modified 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_Export_Init {

	public static function execute( $params ) {
		$blog_id = null;

		// Get subsite Blog ID
		if ( isset( $params['options']['sites'] ) && ( $sites = $params['options']['sites'] ) ) {
			if ( count( $sites ) === 1 ) {
				$blog_id = array_shift( $sites );
			}
		}

		do_action( 'lcm_status_export_init', $params );

		// Set progress
		Lcm_Status::info( __( 'Checking Connection And Preparing For Export...', 'lc-migrator' ), Lcm_Stages::$STAGES['CHECK_CONNECTION'] );

		// Set archive
		if ( empty( $params['archive'] ) ) {
			$params['archive'] = lcm_archive_file( $blog_id );
		}

		// Set storage
		if ( empty( $params['storage'] ) ) {
			$params['storage'] = lcm_storage_folder();
		}

		// Send export started event (non-critical, wrapped in try-catch)
		try {
			$event = new Lcm_Events( 'LC_MIGRATOR_EXPORT_STARTED', array(
				'user_id'  => get_current_user_id(),
				'string_5' => $params['site_id'] ?? 'Unknown site ID',
				'string_1' => get_site_url(),
			) );
			$event->send();
		} catch ( Exception $e ) {
			error_log( 'Lcm_Export_Init: Failed to send event - ' . $e->getMessage() );
		}

		return $params;
	}
}
