<?php
namespace BwWinner\Database;

if ( ! class_exists( __NAMESPACE__ . '\Database' ) ) {
	/**
	 * Manages the database.
	 */
	class Database {
		/**
		 * Add auto update logic to hooks. For BwWinner.
		 * 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() {
			\BwWinner\Options\update_option( 'database_version', '0.0.0' );

			global $wpdb;

			$tables = array(
				$wpdb->prefix . 'bw_winners_entries',
				$wpdb->prefix . 'bw_winners_products',
				$wpdb->prefix . 'bw_winners_brands',
				$wpdb->prefix . 'bw_winners_companies',
				$wpdb->prefix . 'bw_winners_brand_posts'
			);

			foreach ($tables as $table) {
				$wpdb->query( "DROP TABLE IF EXISTS $table" );
			}
		}

		/**
		 * Check for and apply any database updates.
		 */
		public function update() {
			$current_version = \BwWinner\Options\get_option( 'database_version' );

			foreach ( $this->get_updates( $current_version ) as $update ) {
				if ( $this->apply_update( $update ) ) {
					break;
				}
				\BwWinner\Options\update_option( 'database_version', $update['version'] );
			}
		}

		/**
		 * Applies a database update.
		 *
		 * @param array $update    Update version and file.
		 */
		private function apply_update( $update ) {
			$error = include \BwWinner\PLUGIN_PATH . '/includes/database/updates/' . $update['file'];

			if ( $error ) {
				new Database_Update_Error_Notice( $error );
				return true;
			}
		}

		/**
		 * Gets a list of
		 *
		 * @param string $current_version The current verison of the db.
		 *
		 * @return array List of required updates.
		 */
		private function get_updates( $current_version ) {
			return array_filter(
				array(
					array(
						'version' => '1.0.0',
						'file'    => '10000.php',
					),
					array(
						'version' => '1.0.1',
						'file'    => '10001.php',
					),
					array(
						'version' => '1.0.2',
						'file'    => '10002.php',
					),
					array(
						'version' => '1.0.3',
						'file'    => '10003.php',
					),
					array(
						'version' => '1.0.4',
						'file'    => '10004.php',
					),
					array(
						'version' => '1.0.5',
						'file'    => '10005.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 {
		/**
		 * @var string Sql error message.
		 */
		private $error;

		/**
		 * @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-winner' ); ?></p>
					<pre><?php echo esc_html__( $this->error ); ?></pre>
				</div>
			<?php
		}
	}
}
