<?php
/**
 * Plugin Name:       ThemeREX Auto Dealer
 * Plugin URI:        https://themerex.net/trx-auto-dealer/
 * Description:       Professional vehicle listings plugin for WordPress. Supports cars, trucks, motorcycles, boats, RVs, and more. AJAX search, Elementor & Gutenberg integration, WPML support.
 * Version:           1.0.14
 * Requires at least: 6.4
 * Requires PHP:      8.1
 * Author:            ThemeREX
 * Author URI:        https://themerex.net/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       trx-auto-dealer
 * Domain Path:       /languages
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Plugin constants.
define( 'TRX_AD_VERSION', '1.0.14' );
define( 'TRX_AD_FILE', __FILE__ );
define( 'TRX_AD_PATH', plugin_dir_path( __FILE__ ) );
define( 'TRX_AD_URL', plugin_dir_url( __FILE__ ) );
define( 'TRX_AD_BASENAME', plugin_basename( __FILE__ ) );
define( 'TRX_AD_SLUG', 'trx-auto-dealer' );
define( 'TRX_AD_PREFIX', 'trx_ad_' );
define( 'TRX_AD_REST_NAMESPACE', 'trx-auto-dealer/v1' );
define( 'TRX_AD_MIN_PHP', '8.1' );
define( 'TRX_AD_MIN_WP', '6.4' );

// PHP version check.
if ( version_compare( PHP_VERSION, TRX_AD_MIN_PHP, '<' ) ) {
	add_action(
		'admin_notices',
		static function (): void {
			printf(
				'<div class="notice notice-error"><p>%s</p></div>',
				esc_html(
					sprintf(
					/* translators: 1: Required PHP version, 2: Current PHP version. */
						__( 'ThemeREX Auto Dealer requires PHP %1$s or later. You are running PHP %2$s.', 'trx-auto-dealer' ),
						TRX_AD_MIN_PHP,
						PHP_VERSION
					)
				)
			);
		}
	);
	return;
}

// Composer autoloader (if available), otherwise use built-in PSR-4 autoloader.
if ( file_exists( TRX_AD_PATH . 'vendor/autoload.php' ) ) {
	require_once TRX_AD_PATH . 'vendor/autoload.php';
} else {
	spl_autoload_register(
		static function ( string $class ): void {
			$prefix = 'ThemeREX\\AutoDealer\\';
			if ( ! str_starts_with( $class, $prefix ) ) {
				return;
			}
			$relative = substr( $class, strlen( $prefix ) );
			$file     = TRX_AD_PATH . 'includes/' . str_replace( '\\', '/', $relative ) . '.php';
			if ( file_exists( $file ) ) {
				require_once $file;
			}
		}
	);
}

// Load namespaced helper functions (not autoloadable as a class).
require_once TRX_AD_PATH . 'includes/Helpers.php';

/**
 * Bootstrap the plugin.
 *
 * @return void
 */
function trx_ad_init(): void {
	$plugin = new \ThemeREX\AutoDealer\Core\Plugin();
	$plugin->run();
}
add_action( 'plugins_loaded', 'trx_ad_init', 10 );

/**
 * Run on plugin activation.
 *
 * Delegates to Activator which registers CPTs/taxonomies, runs migrations,
 * inserts default terms, creates a default dealer, and flushes rewrite rules.
 *
 * @return void
 */
function trx_ad_activate(): void {
	require_once TRX_AD_PATH . 'includes/Core/Activator.php';
	\ThemeREX\AutoDealer\Core\Activator::activate();
}
register_activation_hook( __FILE__, 'trx_ad_activate' );

/**
 * Run on plugin deactivation.
 *
 * @return void
 */
function trx_ad_deactivate(): void {
	flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'trx_ad_deactivate' );
