<?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_Enumerate_Tables {

	public static function execute( $params ) {
		// Set exclude database
		if ( isset( $params['options']['no_database'] ) ) {
			return $params;
		}

		// Get total tables count
		if ( isset( $params['total_tables_count'] ) ) {
			$total_tables_count = (int) $params['total_tables_count'];
		} else {
			$total_tables_count = 1;
		}

		// Set progress
		Lcm_Status::info( __( 'Gathering database tables...', 'lc-migrator' ), Lcm_Stages::$STAGES['LISTING_EXPORTABLE_CONTENT'] );

		// Get database client
		$db_client = Lcm_Database_Utility::get_client();

		// Include table prefixes
		if ( lcm_table_prefix() ) {
			$db_client->add_table_prefix_filter( lcm_table_prefix() );

			// Include table prefixes (Webba Booking and CiviCRM)
			foreach ( array( 'wbk_', 'civicrm_' ) as $table_name ) {
				$db_client->add_table_prefix_filter( $table_name );
			}
		}

		// Create tables list file
		$tables_list = lcm_open( lcm_tables_list_path( $params ), 'w' );

		// Exclude selected db tables
		$excluded_db_tables = array();
		if ( isset( $params['options']['exclude_db_tables'], $params['excluded_db_tables'] ) ) {
			$excluded_db_tables = explode( ',', $params['excluded_db_tables'] );
		}

		// Write table line
		foreach ( $db_client->get_tables() as $table_name ) {
			if ( ! in_array( $table_name, $excluded_db_tables ) && lcm_putcsv( $tables_list, array( $table_name ) ) ) {
				$total_tables_count++;
			}
		}

		// Include selected db tables
		if ( isset( $params['options']['include_db_tables'] ) && ! empty( $params['included_db_tables'] ) ) {
			foreach ( explode( ',', $params['included_db_tables'] )  as $table_name ) {
				if ( lcm_putcsv( $tables_list, array( $table_name ) ) ) {
					$total_tables_count++;
				}
			}
		}

		// Set progress
		Lcm_Status::info( __( 'Database tables gathered.', 'lc-migrator' ), Lcm_Stages::$STAGES['LISTING_EXPORTABLE_CONTENT'] );

		// Set total tables count
		$params['total_tables_count'] = $total_tables_count;

		// Close the tables list file
		lcm_close( $tables_list );

		return $params;
	}
}
