<?php
// @codingStandardsIgnoreFile

/** Database
 *
 * @package Bw-Winners-Network
 */

namespace BwWinnersNetwork;

if ( ! class_exists( __NAMESPACE__ . '\Database' ) ) {
	/**
	 * Manages the database.
	 */
	class Database {
		/**
		 * Add auto update logic to hooks. For BwWinnersNetwork.
		 * If the plugin will have auto updates enabled use init hook.
		 */
		public function __construct() {
			add_action( 'admin_init', array( $this, 'update' ) );
			add_action( 'rest_api_init', array( $this, 'update' ) );
		}

		/**
		 * Drops the plugins tables. Make sure to keep this up to date.
		 */
		public static function drop_tables() {
			Options::update_option( 'database_version', '0.0.0' );

			global $wpdb;
			// add each table here so it can be removed.
			foreach ( array(
				"DROP TABLE IF EXISTS {$wpdb->prefix}plugin_name_example",
			) as $sql ) {
				$wpdb->query( $sql );
				if ( $wpdb->last_error ) {
					error_log( sprintf( 'Bw-Winners-Network Plugin MySql Error: %s', $wpdb->last_error ) );
				}
			}
		}

		/**
		 * Check for and apply any database updates.
		 */
		public function update() {
			$current_version = Options::get_option( 'database_version' );

			foreach ( $this->get_updates( $current_version ) as $update ) {
				if ( ! $this->apply_update( $update ) ) {
					break;
				}
				Options::update_option( 'database_version', $update['version'] );
			}
		}

		/**
		 * Applies a database update.
		 *
		 * @param array $update    Update version and file.
		 *
		 * @return boolean True iff the update was successful.
		 */
		private function apply_update( $update ) {
			$error = include plugin_dir_path( PLUGIN_PATH ) . '/includes/database/updates/' . $update['file'];

			if ( $error ) {
				new Database_Update_Error_Notice( $error );
				return false;
			}

			return true;
		}

		/**
		 * Gets a list of
		 *
		 * @param string $current_version The current version of the db.
		 *
		 * @return array List of required updates.
		 */
		private function get_updates( $current_version ) {
			return array_filter(
				array(
					array(
						'version' => '0.1.1',
						'file'    => '00101.php',
					),
				),
				function ( $val ) use ( $current_version ) {
					return version_compare( $val['version'], $current_version, 'gt' );
				}
			);
		}
	}
}


if ( ! class_exists( __NAMESPACE__ . '\Database_Update_Error_Notice' ) ) {
	/**
	 * Manages database update error notices.
	 */
	class Database_Update_Error_Notice {
		/**
		 * Sql error message.
		 *
		 * @var string
		 */
		private $error;

		/**
		 * Adds hook.
		 *
		 * @param string $error Sql error message.
		 */
		public function __construct( $error ) {
			$this->error = $error;

			add_action( 'admin_notices', array( $this, 'render' ), 10, 0 );
		}

		/**
		 * WordPress dashboard error notice template
		 */
		public function render() {
			?>
				<div class="notice notice-error is-dismissible">
					<p><?php esc_html__( 'Database Update Error', 'bw-winners-network' ); ?></p>
					<pre><?php echo esc_html( $this->error ); ?></pre>
				</div>
			<?php
		}
	}
}
