<?php

/********************************************************************
 * Copyright (C) 2025 Hassine RIANI (https://upvimeo.com)
 *
 * This file is part of Upvimeo
 *
 * Upvimeo 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 2 of the License, or
 * (at your option) any later version.
 *
 * Upvimeo 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 Upvimeo. If not, see <https://www.gnu.org/licenses/>.
 **********************************************************************/

/**
 * Class UpVimeo_Migrator
 *
 * Responsible for running database migrations
 *
 * @license GPLv2
 * @copyright Hassine RIANI (https://upvimeo.com)
 * @since 1.4.0
 */
class UpVimeo_Migrator
{
	/**
	 * Handles the database upgrade
	 * @return void
	 */
	public function init()
	{
		$settings        = new UpVimeo_Settings_Helper();
		$latest_version  = UPVIMEO_DB_PROVERSION;

		// Try to get version from new settings
		$current_version = $settings->get('pro_version', 0);

		// If 0, check if we have old settings

		// If 0, check if we have old settings
		if ($current_version === 0) {
			// New install or version 99 default
			$current_version = 99;
		}

		if ($current_version < $latest_version) {
			for ($i = $current_version + 1; $i <= $latest_version; $i++) {
				$method = 'upgrade_v' . $i;
				if (method_exists($this, $method)) {
					@$this->$method($settings, $current_version, $i, $latest_version);
				}
			}
		}
	}

	/**
	 * Upgrade database to version
	 *
	 * @param  UpVimeo_Settings_Helper  $settings
	 * @param $new_version
	 * @param $old_version
	 * @param $latest_version
	 *
	 * @return void
	 */
	private function upgrade_v100($settings, $old_version, $new_version, $latest_version)
	{

		$logger = new UpVimeo_Logger();

		// Previous v100 upgrade logic was:
		// $settings->set('enable_embed_presets_management', 1); ...
		// Now we use full keys.
		// However, v100 ran in the past. If we are upgrading FROM < 100, we should set new keys.
		$settings->set('upvimeo_enable_embed_presets_management', 1);
		$settings->set('upvimeo_enable_embed_privacy_management', 1);
		$settings->set('upvimeo_enable_folders_management', 1);
		$settings->set('pro_version', $new_version);
		$settings->save();

		$logger->log(sprintf('Database upgraded from version %s to version %s', $old_version, $new_version), 'UPVIMEO-MIGRATOR');
	}
	/**
	 * Upgrade database to version 101
	 *
	 * Rename options to upvimeo_
	 *
	 * @param  UpVimeo_Settings_Helper  $settings
	 * @param $old_version
	 * @param $new_version
	 * @param $latest_version
	 *
	 * @return void
	 */
	private function upgrade_v101($settings, $old_version, $new_version, $latest_version)
	{

		$logger = new UpVimeo_Logger();

		$settings->set('pro_version', $new_version);
		$settings->save();

		$logger->log(sprintf('Database upgraded from version %s to version %s.', $old_version, $new_version), 'UPVIMEO-MIGRATOR');
	}
}
