<?php
/**
 * Plugin activator.
 *
 * @package OpusPriceBuilder
 */

declare(strict_types=1);

namespace OpusPriceBuilder;

use OpusPriceBuilder\Database\LayoutsTable;

/**
 * Handles plugin activation.
 */
class Activator {

	/**
	 * Run activation tasks.
	 *
	 * @return void
	 */
	public static function activate(): void {
		self::createTables();
		self::runMigrations();
		self::setDefaultOptions();

		// Flush rewrite rules.
		flush_rewrite_rules();

		// Store plugin version.
		update_option( 'oppb_version', OPPB_VERSION );
	}

	/**
	 * Create database tables.
	 *
	 * @return void
	 */
	private static function createTables(): void {
		LayoutsTable::createTable();
	}

	/**
	 * Run database migrations if needed.
	 *
	 * @return void
	 */
	private static function runMigrations(): void {
		LayoutsTable::maybeRunMigrations();
	}


	/**
	 * Set default plugin options.
	 *
	 * @return void
	 */
	private static function setDefaultOptions(): void {
		$defaults = array(
			'oppb_enable_frontend'  => true,
			'oppb_default_scenario' => 'simple',
		);

		foreach ( $defaults as $option => $value ) {
			if ( get_option( $option ) === false ) {
				add_option( $option, $value );
			}
		}
	}
}
