<?php

declare(strict_types=1);

namespace SiteFort\Modules\Cloud\Migrations;

use SiteFort\Core\Database\Migration;
use SiteFort\Modules\Cloud\Service\CloudSettings;
use SiteFort\Modules\Cloud\Service\ConsoleGate;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Grandfathers sites already connected before the gate existed, so they keep working untouched.
 */
final class ConsoleGateMigration implements Migration {

	private const MIGRATED_OPTION = 'sitefort_console_gate_migrated';

	public function module_key(): string {
		return 'console_gate';
	}

	public function target_version(): int {
		return 1;
	}

	public function is_applied(): bool {
		return get_option( self::MIGRATED_OPTION ) === '1';
	}

	public function apply(): bool {
		$settings = CloudSettings::get_all();

		if ( ! empty( $settings['console_enabled'] ) ) {
			ConsoleGate::mark_grandfathered();
		}

		update_option( self::MIGRATED_OPTION, '1', false );

		return true;
	}
}
