<?php
/**
 * Plugin activation.
 *
 * filename: class-rc-activator.php
 *
 * @package RestaurantCosting
 */

declare(strict_types=1);

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

/**
 * Runs on plugin activation: tables + FoodMaster import.
 */
final class RC_Activator {

	/**
	 * Activate plugin.
	 */
	public static function activate(): void {
		if ( version_compare( PHP_VERSION, '8.2.0', '<' ) ) {
			deactivate_plugins( plugin_basename( RC_PLUGIN_FILE ) );
			wp_die(
				esc_html__( 'Restaurant Costing requires PHP 8.2 or newer.', 'restaurant-costing' ),
				esc_html__( 'Plugin Activation Error', 'restaurant-costing' ),
				array( 'back_link' => true )
			);
		}

		RC_Database::create_tables();
		update_option( 'rc_db_version', RC_DB_VERSION );

		RC_Roles::register_roles();
		update_option( 'rc_roles_version', RC_ROLES_VERSION );

		$importer = new RC_Importer();
		$result   = $importer->import_foodmaster_if_empty();

		RC_Database::seed_food_types();
		RC_Database::seed_suppliers();

		update_option(
			'rc_activation_import',
			array(
				'ran_at'  => current_time( 'mysql' ),
				'result'  => $result,
				'skipped' => ! empty( $result['skipped'] ),
			),
			false
		);
	}
}
