<?php

namespace FuseCommerce\Config;

/**
 * Configuration constants for the product indexing system
 */
class IndexingConfig {
    /**
     * Number of products to process per batch
     */
    const INDEXING_BATCH_SIZE = 30;

    /**
     * Use 70% of max_execution_time as safety buffer
     */
    const INDEXING_MAX_EXECUTION_TIME_RATIO = 0.7;

    /**
     * WordPress option key for storing indexing state
     */
    const INDEXING_STATE_OPTION_KEY = 'fusecommerce_indexing_state';

    /**
     * WordPress option key for storing last indexing state
     */
    const INDEXING_LAST_STATE_OPTION_KEY = 'fusecommerce_indexing_last_state';

    /**
     * Image size to use for product images
     */
    const PRODUCT_IMAGE_SIZE = 'medium';

    /**
     * Directory name within wp-content/uploads
     */
    const UPLOAD_DIR_NAME = 'fusecommerce';

    /**
     * JSON file names
     */
    const PRODUCTS_JSON_FILE = 'products.json';
    const PRODUCTS_TEMP_JSON_FILE = 'products-temp.json';
    const CATEGORIES_JSON_FILE = 'categories.json';
    const ATTRIBUTES_JSON_FILE = 'attributes.json';
    const TAGS_JSON_FILE = 'tags.json';

    /**
     * Get the base directory path for storing JSON files
     *
     * @return string Full path to storage directory
     */
    public static function getStorageDir(): string {
        $upload_dir = wp_upload_dir();
        return trailingslashit( $upload_dir['basedir'] ) . self::UPLOAD_DIR_NAME;
    }

    /**
     * Get the base URL for accessing JSON files
     *
     * @return string URL to storage directory
     */
    public static function getStorageUrl(): string {
        $upload_dir = wp_upload_dir();
        return trailingslashit( $upload_dir['baseurl'] ) . self::UPLOAD_DIR_NAME;
    }

    /**
     * Get the maximum execution time in seconds
     *
     * @return int Maximum execution time with safety buffer applied
     */
    public static function getMaxExecutionTime(): int {
        $max_time = ini_get( 'max_execution_time' );

        // If unlimited (0) or not set, use 30 seconds as default
        if ( empty( $max_time ) ) {
            $max_time = 30;
        }

        return (int) ( $max_time * self::INDEXING_MAX_EXECUTION_TIME_RATIO );
    }

    /**
     * Get full path to a JSON file
     *
     * @param string $filename File name constant
     * @return string Full file path
     */
    public static function getFilePath( string $filename ): string {
        return trailingslashit( self::getStorageDir() ) . $filename;
    }

    /**
     * Get URL to a JSON file
     *
     * @param string $filename File name constant
     * @return string Full file URL
     */
    public static function getFileUrl( string $filename ): string {
        return trailingslashit( self::getStorageUrl() ) . $filename;
    }
}
