<?php
/**
 * Plugin Name:       Restaurant Costing
 * Plugin URI:        https://itoversight.ca
 * Description:       Meal and drink costing, margins, POS import, and profit/loss for restaurants.
 * Version:           1.2.4
 * Requires at least: 7.0
 * Requires PHP:      8.2
 * Author:            Steve Vogelaar, IT Oversight
 * License:           GPL-2.0-or-later
 * Text Domain:       restaurant-costing
 *
 * @package RestaurantCosting
 *
 * Init Order (Dependency Map):
 *   1. RC_Autoloader    — PSR-4-style autoloader for all RC_* classes
 *   2. RC_Build_Hash    — conditional; license verification constants
 *   3. RC_Activator     — activation hook; DB schema creation
 *   4. RC_Deactivator   — deactivation hook; cleanup
 *   5. plugins_loaded   → RC_Plugin::instance()->init()
 *      ├── RC_Database::maybe_upgrade()      — schema migrations
 *      ├── RC_Roles::register_roles()        — user capabilities
 *      ├── RC_Admin::instance()->init()      — menus, assets, AJAX handlers
 *      │   ├── Repositories (static)         — RC_Ingredient_Repository, RC_Recipe_Repository,
 *      │   │   RC_Supplier_Repository, RC_Area_Repository, RC_Inventory_Repository,
 *      │   │   RC_Food_Type_Repository, RC_Menu_Type_Repository, RC_Restaurant_Repository
 *      │   ├── RC_Recipe_Calculator (static) — cost/nutrition calculations
 *      │   ├── RC_Notice (static)            — admin notices
 *      │   ├── RC_License (static)           — license/demo checks
 *      │   ├── RC_Unit_Converter (static)    — unit conversions
 *      │   └── RC_Name_Matcher (static)      — normalized name matching
 *      └── load_plugin_textdomain()          — i18n
 */

declare(strict_types=1);

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

define( 'RC_VERSION', '1.2.4' );
define( 'RC_PLUGIN_FILE', __FILE__ );
define( 'RC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'RC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'RC_DB_VERSION', '11' );
define( 'RC_ROLES_VERSION', '1' );
define( 'RC_LICENSE_SALT', 'rc7' );

require_once RC_PLUGIN_DIR . 'includes/class-rc-autoloader.php';
RC_Autoloader::register();

if ( file_exists( RC_PLUGIN_DIR . 'includes/class-rc-build-hash.php' ) ) {
	require_once RC_PLUGIN_DIR . 'includes/class-rc-build-hash.php';
}

register_activation_hook( __FILE__, array( 'RC_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'RC_Deactivator', 'deactivate' ) );

function rc_fmt_num( $value ): string {
	if ( '' === $value || null === $value ) {
		return '';
	}
	$n = (float) $value;
	if ( $n == (int) $n ) {
		return (string) (int) $n;
	}
	return rtrim( rtrim( number_format( $n, 6, '.', '' ), '0' ), '.' );
}

add_action(
	'plugins_loaded',
	static function (): void {
		RC_Plugin::instance()->init();
	}
);
