<?php
/**
 * Plugin constants class.
 *
 * @package SaveToWishlist\Classes
 * @since   1.0.0
 */

namespace SaveToWishlist\Classes;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Class Constants
 *
 * Defines and centralises all plugin-wide constants. This class is
 * intentionally non-instantiable; all interaction is through the
 * static ::define() entry-point.
 */
class Constants {

    /**
     * Private constructor — prevents instantiation.
     */
    private function __construct() {}

    /**
     * Define all plugin constants.
     *
     * Must be called from the main plugin file so that $plugin_file
     * resolves to the correct entry-point path.
     *
     * @since 1.0.0
     *
     * @param string $plugin_file Absolute path to the main plugin file.
     */
    public static function define( string $plugin_file ): void {
        self::set( 'STWLITE_VERSION', get_plugin_data( $plugin_file, false, false )['Version'] );
        self::set( 'STWLITE_DB_VERSION', '1.0.0' );
        self::set( 'STWLITE_PLUGIN_FILE', $plugin_file );
        self::set( 'STWLITE_PLUGIN_DIR', plugin_dir_path( $plugin_file ) );
        self::set( 'STWLITE_PLUGIN_URL', plugin_dir_url( $plugin_file ) );
        self::set( 'STWLITE_BASENAME', plugin_basename( $plugin_file ) );
        self::set( 'STWLITE_JS_URL', plugin_dir_url( $plugin_file ) . 'assets/js/' );
        self::set( 'STWLITE_CSS_URL', plugin_dir_url( $plugin_file ) . 'assets/css/' );
        self::set( 'STWLITE_IMG_URL', plugin_dir_url( $plugin_file ) . 'assets/images/' );
        self::set( 'STWLITE_IMG_PATH', plugin_dir_path( $plugin_file ) . 'assets/images/' );
        self::set( 'STWLITE_VIEWS_DIR', plugin_dir_path( $plugin_file ) . 'views/' );
        self::set( 'STWLITE_TEMPLATES_DIR', plugin_dir_path( $plugin_file ) . 'templates/' );
        self::set( 'STWLITE_CACHE_GROUP', 'stwlite_cache_group' );
        self::set( 'STWLITE_IMPORT_RATE_LIMIT', 300 );
        self::set( 'STWLITE_LAST_IMPORT_TIME_GROUP', 'stwlite_last_import_time_' );
        self::set( 'STWLITE_IMPORT_PROGRESS_GROUP', 'stwlite_import_progress_' );
    }

    /**
     * Define a global constant only if it has not already been defined.
     *
     * Guarding with defined() prevents fatal errors when a companion or
     * pro plugin defines a constant before this plugin loads.
     *
     * @since 1.0.0
     *
     * @param string $name  Constant name.
     * @param mixed  $value Constant value.
     */
    private static function set( string $name, $value ): void {
        if ( ! defined( $name ) ) {
            define( $name, $value );
        }
    }
}
