<?php
/**
 * Plugin Name: JB Planeación
 * Plugin URI:  https://latingraf.com/
 * Description: Gestiona inventario bodega, calculadoras y cierre de órdenes.
 * Versión:     1.0.44
 * Author:      Jorge Borja
 * Text Domain: logistica
 * Domain Path: /languages
 *
 * @package Logistica
 */

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

define( 'LOGISTICA_VERSION', '1.6.0' );
define( 'LOGISTICA_PLUGIN_FILE', __FILE__ );
define( 'LOGISTICA_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'LOGISTICA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'LOGISTICA_VIEWS_DIR', LOGISTICA_PLUGIN_DIR . 'views/' );
define( 'LOGISTICA_VIEWS_SHELLS', LOGISTICA_VIEWS_DIR . 'shells/' );
define( 'LOGISTICA_VIEWS_FRONTEND', LOGISTICA_VIEWS_DIR . 'frontend/' );
define( 'LOGISTICA_VIEWS_ADMIN', LOGISTICA_VIEWS_DIR . 'admin/' );
define( 'LOGISTICA_VIEWS_SHARED', LOGISTICA_VIEWS_DIR . 'shared/' );

/**
 * Versión string for enqueued assets (mtime busts browser cache on file change).
 *
 * @param string $relative_path Path relative to the plugin root.
 * @return string
 */
function logistica_asset_version( $relative_path ) {
	$file = LOGISTICA_PLUGIN_DIR . ltrim( $relative_path, '/' );
	if ( is_readable( $file ) ) {
		return (string) filemtime( $file ) . '-' . LOGISTICA_VERSION;
	}

	return LOGISTICA_VERSION;
}

/**
 * Imprime favicon / icono de la app en el <head>.
 *
 * @return void
 */
function logistica_print_favicon() {
	$relative = 'assets/images/favicon.svg';
	$url      = function_exists( 'home_url' )
		? home_url( $relative )
		: ( LOGISTICA_PLUGIN_URL . $relative );
	$url      = add_query_arg( 'v', logistica_asset_version( $relative ), $url );
	$app_name = 'JB Planeación';
	if ( class_exists( 'JB_App', false ) ) {
		$app_name = (string) JB_App::config( 'app_name' );
	}

	printf(
		'<link rel="icon" href="%1$s" type="image/svg+xml">' . "\n" .
		'<link rel="apple-touch-icon" href="%1$s">' . "\n" .
		'<link rel="shortcut icon" href="%1$s">' . "\n" .
		'<meta name="theme-color" content="#0f766e">' . "\n" .
		'<meta name="application-name" content="%2$s">' . "\n",
		esc_url( $url ),
		esc_attr( $app_name )
	);
}

require_once __DIR__ . '/includes/class-logistica-database.php';
require_once __DIR__ . '/includes/class-logistica-activator.php';
require_once __DIR__ . '/includes/class-logistica-inventory.php';
require_once __DIR__ . '/includes/class-logistica-conversions.php';
require_once __DIR__ . '/includes/class-logistica-conversion-tables.php';
require_once __DIR__ . '/includes/class-logistica-warehouse-inventory.php';
require_once __DIR__ . '/includes/class-logistica-order-closing.php';
require_once __DIR__ . '/includes/class-logistica-order-closing-characteristics.php';
require_once __DIR__ . '/includes/class-logistica-order-closing-characteristics-excel.php';
require_once __DIR__ . '/includes/class-logistica-order-closing-bulk-excel.php';
require_once __DIR__ . '/includes/class-logistica-warehouse-inventory-excel.php';
require_once __DIR__ . '/includes/class-logistica-settings.php';
require_once __DIR__ . '/includes/class-logistica-select-catalog.php';
require_once __DIR__ . '/includes/class-logistica-select-catalog-usage.php';
require_once __DIR__ . '/includes/class-logistica-tabs.php';
require_once __DIR__ . '/includes/class-logistica-roles.php';
require_once __DIR__ . '/includes/class-logistica-colombia-holidays.php';
require_once __DIR__ . '/includes/class-logistica-print-planning.php';
require_once __DIR__ . '/includes/class-logistica-print-planning-excel.php';
require_once __DIR__ . '/includes/class-logistica-forming-planning.php';
require_once __DIR__ . '/includes/class-logistica-forming-yield.php';
require_once __DIR__ . '/includes/class-logistica-forming-planning-excel.php';
require_once __DIR__ . '/includes/class-logistica-planning-excel-import.php';
require_once __DIR__ . '/includes/class-logistica-planning-metrics.php';
require_once __DIR__ . '/includes/class-logistica-user-panel.php';
require_once __DIR__ . '/includes/class-logistica-user-panel-proveedores-excel.php';
require_once __DIR__ . '/includes/class-logistica-user-panel-compras-excel.php';
require_once __DIR__ . '/includes/class-logistica-label-barcode.php';
require_once __DIR__ . '/includes/class-logistica-label-product-catalog.php';
require_once __DIR__ . '/includes/class-logistica-label-product-catalog-excel.php';
require_once __DIR__ . '/includes/class-logistica-label-etiqueta-tamanos.php';
require_once __DIR__ . '/includes/class-logistica-label-etiqueta-tipos.php';
require_once __DIR__ . '/includes/class-logistica-label-etiqueta-mercados.php';
require_once __DIR__ . '/includes/class-logistica-label-etiqueta-option-usage.php';
require_once __DIR__ . '/includes/class-logistica-label-logo-history.php';
require_once __DIR__ . '/includes/class-logistica-label-print.php';
require_once __DIR__ . '/includes/class-logistica-label-pack-history.php';
require_once __DIR__ . '/includes/class-logistica-rotulo-rollos-print.php';
require_once __DIR__ . '/includes/class-logistica-rotulo-rollos-print-history.php';
require_once __DIR__ . '/includes/class-logistica-rotulo-rollos-catalog.php';
require_once __DIR__ . '/includes/class-logistica-rotulo-rollos-catalog-excel.php';
require_once __DIR__ . '/includes/class-logistica-pedidos-board.php';
require_once __DIR__ . '/includes/class-logistica-pedidos-board-excel.php';
require_once __DIR__ . '/includes/class-logistica-pedidos-planeacion-bridge.php';
require_once __DIR__ . '/includes/class-logistica-pedidos-metrics.php';
require_once __DIR__ . '/includes/class-logistica-minutas.php';
require_once __DIR__ . '/includes/class-logistica-minutas-excel.php';
require_once __DIR__ . '/includes/class-logistica-module-registry.php';
require_once __DIR__ . '/includes/class-logistica-audit.php';
require_once __DIR__ . '/includes/class-logistica-trash.php';
require_once __DIR__ . '/includes/class-logistica-version-history.php';
require_once __DIR__ . '/includes/class-logistica-soft-delete.php';
require_once __DIR__ . '/includes/class-logistica-entity-lifecycle.php';
require_once __DIR__ . '/includes/class-logistica-option-lifecycle.php';
require_once __DIR__ . '/includes/class-logistica-label-option-lifecycle.php';
require_once __DIR__ . '/includes/class-logistica-lifecycle-hooks.php';
require_once __DIR__ . '/includes/class-logistica-user-lifecycle.php';
require_once __DIR__ . '/includes/class-logistica-backup.php';
require_once __DIR__ . '/includes/class-logistica-help.php';
require_once __DIR__ . '/includes/class-logistica-admin-menu.php';
require_once __DIR__ . '/includes/class-logistica-frontend.php';
require_once __DIR__ . '/includes/class-logistica-backend.php';
require_once __DIR__ . '/includes/class-logistica-panel-auth.php';

register_activation_hook( __FILE__, array( 'Logistica_Activator', 'activate' ) );

/**
 * Creates or updates DB tables when the plugin version changes.
 *
 * @return void
 */
function logistica_maybe_upgrade_db() {
	$installed = get_option( 'logistica_version', '' );

	if ( version_compare( (string) $installed, LOGISTICA_VERSION, '<' ) ) {
		Logistica_Database::create_tables();
		update_option( 'logistica_version', LOGISTICA_VERSION );
	}

	Logistica_Database::migrate_inventario_bodega_codigo_lote();
	Logistica_Database::ensure_cierre_caracteristicas_table();
	Logistica_Database::ensure_planeacion_impresion_table();
	Logistica_Database::ensure_planeacion_formado_table();
	Logistica_Database::ensure_etiqueta_productos_table();
	Logistica_Database::ensure_etiqueta_pack_historial_table();
	Logistica_Database::ensure_rotulo_rollos_table();
	Logistica_Database::ensure_rotulo_rollos_print_historial_table();
	Logistica_Database::ensure_pedidos_board_tables();
	Logistica_Database::ensure_minutas_table();
	Logistica_Database::migrate_planeacion_day_hours();
	Logistica_Database::ensure_lifecycle_tables();
	Logistica_Soft_Delete::migrate_all_columns();

	if ( ! get_option( 'logistica_roles_initialized', false ) ) {
		Logistica_Roles::register_roles();
		update_option( 'logistica_roles_initialized', true, false );
	}
}
add_action( 'plugins_loaded', 'logistica_maybe_upgrade_db', 0 );

/**
 * Starts the plugin once WordPress has loaded its core APIs.
 */
function logistica_bootstrap() {
	Logistica_Roles::init();
	Logistica_Entity_Lifecycle::init();
	Logistica_Lifecycle_Hooks::init();
	Logistica_User_Lifecycle::init();
	Logistica_Backup::init();
	Logistica_Panel_Auth::init();
	Logistica_Backend::init();

	/** @disregard P1009 Archivo de clase grande; Intelephense a veces no lo indexa. */
	$admin_menu = new Logistica_Admin_Menu();
	$admin_menu->init_request_handlers();

	$frontend = new Logistica_Frontend();
	$frontend->init();

	if ( is_admin() ) {
		$admin_menu->init();
	}
}
add_action( 'plugins_loaded', 'logistica_bootstrap' );
