<?php


namespace Fliegerlogin\DbSchema;


use Fliegerlogin\Options;

class Users implements IDbSchema {

	private static $column_vf_user_id = 'vf_uid';
	private static $unique_vf_user_id = 'vf_uid';

	/**
	 * The prefix these names carried up to version 3.0.0.
	 *
	 * Version 3.0.1 renamed every prefix in the plugin to fliegerlogin_ so that
	 * nothing can collide with another plugin. The column in the users table was
	 * renamed with it - but it already existed in every installation, and no
	 * migration came along. The result was a query against a column that is not
	 * there, and with it a failed login for every linked member.
	 */
	private const LEGACY_PREFIX = 'wpvf_';

	private static $column_definition = 'INT NULL DEFAULT NULL';

	public static function install(): void {
		$dbVersion = Options::getInstalledDbVersion();

		if ($dbVersion === null || $dbVersion < 1) {
			DbSchema::addColumn(self::getTableName(), self::getColumnVfUserId(), self::$column_definition);
			DbSchema::addUnique(self::getTableName(), self::getUniqueVfUserId(), [self::getColumnVfUserId()]);

			return;
		}

		if ($dbVersion < 2) {
			self::renameLegacyColumn();
		}
	}

	/**
	 * Moves an installation from the wpvf_ names to the current ones.
	 *
	 * Renaming rather than adding: the column holds the link between WordPress
	 * accounts and Vereinsflieger members. Dropping it and starting over would
	 * unlink every member, and the next login would create a second account for
	 * each of them.
	 */
	private static function renameLegacyColumn(): void {
		$table = self::getTableName();
		$legacyColumn = self::LEGACY_PREFIX . self::$column_vf_user_id;
		$column = self::getColumnVfUserId();

		// Nothing to do when the rename already happened, and nothing to do when
		// there is no old column either - an installation set up fresh on 3.0.0
		// carries the new name from the start.
		if ($column === $legacyColumn || !DbSchema::columnExists($table, $legacyColumn)) {
			return;
		}

		if (DbSchema::columnExists($table, $column)) {
			return;
		}

		DbSchema::renameColumn($table, $legacyColumn, $column, self::$column_definition);

		// The unique index carries the prefix as well. Dropping and recreating
		// instead of RENAME INDEX, which older MariaDB does not know.
		$legacyIndex = self::LEGACY_PREFIX . self::$unique_vf_user_id;
		if (DbSchema::indexExists($table, $legacyIndex)) {
			DbSchema::removeIndex($table, $legacyIndex);
		}
		if (!DbSchema::indexExists($table, self::getUniqueVfUserId())) {
			DbSchema::addUnique($table, self::getUniqueVfUserId(), [$column]);
		}
	}

	public static function uninstall(): void {
		$dbVersion = Options::getInstalledDbVersion();

		if ($dbVersion >= 1) {
			DbSchema::removeColumn(self::getTableName(), self::getColumnVfUserId());
			DbSchema::removeIndex(self::getTableName(), self::getUniqueVfUserId());
		}
	}

	/**
	 * Gets the table name of the users table.
	 * @return string
	 */
	public static function getTableName(): string {
		return DbSchema::getWpdb()->users;
	}

	/**
	 * Gets the column name for the vereinsflieger.de user id.
	 * @return string
	 */
	public static function getColumnVfUserId(): string {
		return DbSchema::getTablePrefix() . self::$column_vf_user_id;
	}

	/**
	 * Gets the index name for the unique index of column vereinsflieger.de user id.
	 * @return string
	 */
	public static function getUniqueVfUserId(): string {
		return DbSchema::getTablePrefix() . self::$unique_vf_user_id;
	}

}