<?php

namespace Lcmigrator\LcMigrator\Migrator\Database;

use Lcmigrator\LcMigrator\Database\DatabaseUtilityFunctions;
use Lcmigrator\LcMigrator\Logger\Logger;

class DatabaseExporter {

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

		// Set query offset
		if ( isset( $params['query_offset'] ) ) {
			$query_offset = (int) $params['query_offset'];
		} else {
			$query_offset = 0;
		}

		// Set table index
		if ( isset( $params['table_index'] ) ) {
			$table_index = (int) $params['table_index'];
		} else {
			$table_index = 0;
		}

		// Set table offset
		if ( isset( $params['table_offset'] ) ) {
			$table_offset = (int) $params['table_offset'];
		} else {
			$table_offset = 0;
		}

		// Set table rows
		if ( isset( $params['table_rows'] ) ) {
			$table_rows = (int) $params['table_rows'];
		} else {
			$table_rows = 0;
		}

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

		// What percent of tables have we processed?
		$progress = (int) ( ( $table_index / $total_tables_count ) * 100 );


		// Get database client
		$db_client = DatabaseUtilityFunctions::create_client();

		// Exclude spam comments
		if ( isset( $params['options']['no_spam_comments'] ) ) {
			$db_client->set_table_where_query( lc_migrator_table_prefix() . 'comments', "`comment_approved` != 'spam'" )
				->set_table_where_query(  lc_migrator_table_prefix() . 'commentmeta', sprintf( "`comment_ID` IN ( SELECT `comment_ID` FROM `%s` WHERE `comment_approved` != 'spam' )",  lc_migrator_table_prefix() . 'comments' ) );
		}

		// Exclude post revisions
		if ( isset( $params['options']['no_post_revisions'] ) ) {
			$db_client->set_table_where_query(  lc_migrator_table_prefix() . 'posts', "`post_type` != 'revision'" );
		}

		$old_table_prefixes = $old_column_prefixes = array();
		$new_table_prefixes = $new_column_prefixes = array();

		// Set table prefixes
		if (  lc_migrator_table_prefix() ) {
			$old_table_prefixes[] =  lc_migrator_table_prefix();
			$new_table_prefixes[] = lc_migrator_db_prefix();
		}

		// Set column prefixes
		if ( strlen(  lc_migrator_table_prefix() ) > 1 ) {
			$old_column_prefixes[] =  lc_migrator_table_prefix();
			$new_column_prefixes[] = lc_migrator_db_prefix();
		} else {
			foreach ( array( 'user_roles', 'capabilities', 'user_level', 'dashboard_quick_press_last_post_id', 'user-settings', 'user-settings-time' ) as $column_prefix ) {
				$old_column_prefixes[] =  lc_migrator_table_prefix() . $column_prefix;
				$new_column_prefixes[] = lc_migrator_db_prefix() . $column_prefix;
			}
		}

		$db_client
			->set_old_table_prefixes( $old_table_prefixes )
			->set_new_table_prefixes( $new_table_prefixes )
			->set_old_column_prefixes( $old_column_prefixes )
			->set_new_column_prefixes( $new_column_prefixes );

		// Exclude column prefixes
		$db_client->set_reserved_column_prefixes( array( 'wp_force_deactivated_plugins', 'wp_page_for_privacy_policy', 'wp_rocket_settings', 'wp_rocket_dismiss_imagify_notice', 'wp_rocket_no_licence', 'wp_rocket_rocketcdn_old_url', 'wp_rocket_hide_deactivation_form' ) );


		// Set table select columns
		if ( ( $column_names = $db_client->get_column_names(  lc_migrator_table_prefix() . 'options' ) ) ) {
			if ( isset( $column_names['option_name'], $column_names['option_value'] ) ) {
				$column_names['option_value'] = sprintf( "(CASE WHEN option_name = '%s' THEN 'a:0:{}' WHEN (option_name = '%s' OR option_name = '%s') THEN '' ELSE option_value END) AS option_value", LC_MIGRATOR_ACTIVE_PLUGINS, LC_MIGRATOR_ACTIVE_TEMPLATE, LC_MIGRATOR_ACTIVE_STYLESHEET );
			}

			$db_client->set_table_select_columns(  lc_migrator_table_prefix() . 'options', $column_names );
		}

		// Set table prefix columns
		$db_client->set_table_prefix_columns(  lc_migrator_table_prefix() . 'options', array( 'option_name' ) )
			->set_table_prefix_columns(  lc_migrator_table_prefix() . 'usermeta', array( 'meta_key' ) );

        $logger = Logger::getInstance();
        $logger->info("Exporting database..."); 
        $logger->info(json_encode($params));
		// Export database
		if ( $db_client->export( $params['db_path'], $query_offset, $table_index, $table_offset, $table_rows ) ) {

			// Unset query offset
			unset( $params['query_offset'] );

			// Unset table index
			unset( $params['table_index'] );

			// Unset table offset
			unset( $params['table_offset'] );

			// Unset table rows
			unset( $params['table_rows'] );

			// Unset total tables count
			unset( $params['total_tables_count'] );

			// Unset completed flag
			unset( $params['completed'] );

		} else {

			// What percent of tables have we processed?
			$progress = (int) ( ( $table_index / $total_tables_count ) * 100 );

			// Set query offset
			$params['query_offset'] = $query_offset;

			// Set table index
			$params['table_index'] = $table_index;

			// Set table offset
			$params['table_offset'] = $table_offset;

			// Set table rows
			$params['table_rows'] = $table_rows;

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

			// Set completed flag
			$params['completed'] = false;
		}

		return $params;
	}
}
