<?php

namespace AIPT\Api\Constants;

/**
 * Automation Jobs module constants with apply_filters hooks for site-level overrides.
 *
 * Filter names are public API as of v2.6.0 — do NOT rename them.
 *
 * @package AIPT\Api\Constants
 * @since   2.6.0
 */
class AutomationConstants {

    /**
     * Maximum batch size for processing products per tick.
     *
     * Filter: aipt_automation_max_batch_size
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_BATCH_SIZE = 10;

    /**
     * Maximum execution time per batch in seconds.
     *
     * Filter: aipt_automation_max_batch_execution_time
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_BATCH_EXECUTION_TIME = 60;

    /**
     * Time-sliced tick budget in seconds.
     *
     * Filter: aipt_automation_tick_time_budget
     *
     * @since 2.6.0
     * @var int
     */
    public const TICK_TIME_BUDGET = 25;

    /**
     * Default window size for claimed items per tick.
     *
     * Filter: aipt_automation_window_size
     *
     * @since 2.6.0
     * @var int
     */
    public const WINDOW_SIZE = 15;

    /**
     * Minimum window size (floor for adaptive sizing).
     *
     * @since 2.6.0
     * @var int
     */
    public const MIN_WINDOW_SIZE = 5;

    /**
     * Maximum window size (ceiling for adaptive sizing).
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_WINDOW_SIZE = 30;

    /**
     * Delay in seconds before scheduling next tick when there is more work.
     *
     * Filter: aipt_automation_reschedule_delay
     *
     * @since 2.6.0
     * @var int
     */
    public const RESCHEDULE_DELAY = 2;

    /**
     * Per-job execution lock TTL in seconds.
     *
     * Filter: aipt_automation_lock_ttl
     *
     * @since 2.6.0
     * @var int
     */
    public const LOCK_TTL = 120;

    /**
     * Buffer size before flushing progress increments.
     *
     * @since 2.6.0
     * @var int
     */
    public const PROGRESS_FLUSH_EVERY = 5;

    /**
     * Chunk size for pool building.
     *
     * @since 2.6.0
     * @var int
     */
    public const POOL_BUILD_CHUNK_SIZE = 5000;

    /**
     * Interval (items processed) between gc_collect_cycles() calls.
     *
     * @since 2.6.0
     * @var int
     */
    public const GC_COLLECT_CYCLES_INTERVAL = 10;

    /**
     * Delay in seconds for immediate execution scheduling.
     *
     * Filter: aipt_automation_immediate_execution_delay
     *
     * @since 2.6.0
     * @var int
     */
    public const IMMEDIATE_EXECUTION_DELAY = 10;

    /**
     * Default maximum title length in characters.
     *
     * @since 2.6.0
     * @var int
     */
    public const DEFAULT_TITLE_MAX_LENGTH = 70;

    /**
     * Maximum error message length in characters.
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_ERROR_MESSAGE_LENGTH = 500;

    /**
     * Default memory limit in megabytes (fallback when PHP limit is unavailable).
     *
     * @since 2.6.0
     * @var int
     */
    public const DEFAULT_MEMORY_LIMIT_MB = 100;

    /**
     * Maximum automation jobs allowed to run concurrently.
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_CONCURRENT_JOBS = 3;

    /**
     * Running job timeout in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const JOB_TIMEOUT = 1800;

    /**
     * Error count threshold before adaptive throttling kicks in.
     *
     * @since 2.6.0
     * @var int
     */
    public const ERROR_THROTTLE_THRESHOLD = 5;

    /**
     * Minimum execution time in seconds before throttling is applied.
     *
     * @since 2.6.0
     * @var int
     */
    public const MIN_EXECUTION_TIME_SECONDS = 30;

    /**
     * Error throttle window in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const ERROR_THROTTLE_WINDOW_SECONDS = 300;

    /**
     * Debug snapshot transient TTL in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const DEBUG_SNAPSHOT_TTL = 120;

    /**
     * Lock token length (characters) for wp_generate_password().
     *
     * @since 2.6.0
     * @var int
     */
    public const LOCK_TOKEN_LENGTH = 20;

    /**
     * Maximum sample size for completion estimation.
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_SAMPLE_SIZE = 50;

    /**
     * Minimum sample size for completion estimation.
     *
     * @since 2.6.0
     * @var int
     */
    public const MIN_SAMPLE_SIZE = 10;

    /**
     * Maximum completion estimate in days.
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_COMPLETION_ESTIMATE_DAYS = 30;

    /**
     * Rate limit for create_job endpoint (requests per minute).
     *
     * @since 2.6.0
     * @var int
     */
    public const RATE_LIMIT_CREATE_JOB = 10;

    /**
     * Rate limit for retry_failed endpoint (requests per minute).
     *
     * @since 2.6.0
     * @var int
     */
    public const RATE_LIMIT_RETRY_FAILED = 10;

    /**
     * Rate limit for process_jobs endpoint (requests per minute).
     *
     * @since 2.6.0
     * @var int
     */
    public const RATE_LIMIT_PROCESS_JOBS = 30;

    /**
     * Rate limit for trigger_job endpoint (requests per minute).
     *
     * @since 2.6.0
     * @var int
     */
    public const RATE_LIMIT_TRIGGER_JOB = 5;

    /**
     * Delay in seconds for trigger execution scheduling.
     *
     * @since 2.6.0
     * @var int
     */
    public const TRIGGER_EXECUTION_DELAY = 5;

    /**
     * Maximum trigger execution attempts.
     *
     * @since 2.6.0
     * @var int
     */
    public const TRIGGER_EXECUTION_ATTEMPTS = 5;

    /**
     * Product count cache TTL in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const PRODUCT_COUNT_CACHE_TTL = 300;

    /**
     * Default product count when cache is unavailable.
     *
     * @since 2.6.0
     * @var int
     */
    public const DEFAULT_PRODUCT_COUNT = 10;

    /**
     * Monitor cache TTL in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const MONITOR_CACHE_TTL = 5;

    /**
     * Rate limit monitor window in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const RATE_LIMIT_MONITOR_WINDOW = 120;

    /**
     * Browser lock TTL in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const BROWSER_LOCK_TTL = 120;

    /**
     * Execution event grace period in seconds before monitor force-reschedules.
     *
     * @since 2.6.0
     * @var int
     */
    public const EXECUTION_EVENT_GRACE_PERIOD = 120;

    /**
     * Maximum number of preparing jobs to build pools for per tick.
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_PREPARING_JOBS_TO_BUILD = 5;

    /**
     * Maximum pool build chunks per tick.
     *
     * @since 2.6.0
     * @var int
     */
    public const MAX_POOL_BUILD_CHUNKS = 3;

    /**
     * Initial execution delay in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const INITIAL_EXECUTION_DELAY = 10;

    /**
     * Stale claim age in minutes before reclaim.
     *
     * @since 2.6.0
     * @var int
     */
    public const STALE_CLAIM_MINUTES = 15;

    /**
     * Fallback schedule delay in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const FALLBACK_SCHEDULE_DELAY = 60;

    /**
     * Default ready jobs limit for execution.
     *
     * @since 2.6.0
     * @var int
     */
    public const DEFAULT_READY_JOBS_LIMIT = 10;

    /**
     * Log throttle cadence in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const LOG_THROTTLE_CADENCE_SECONDS = 300;

    /**
     * Snapshot log TTL in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const SNAPSHOT_LOG_TTL = 300;

    /**
     * Process jobs cron interval in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const PROCESS_JOBS_CRON_INTERVAL = 60;

    /**
     * Monitor jobs cron interval in seconds.
     *
     * @since 2.6.0
     * @var int
     */
    public const MONITOR_JOBS_CRON_INTERVAL = 30;

    /**
     * Get the effective maximum batch size.
     *
     * @since 2.6.0
     * @return int
     */
    public static function maxBatchSize(): int {
        /**
         * Filter the maximum batch size for automation job processing.
         *
         * @since 2.6.0
         * @param int $size Default maximum batch size.
         */
        $value = (int) apply_filters( 'aipt_automation_max_batch_size', self::MAX_BATCH_SIZE );
        return $value >= 1 ? $value : self::MAX_BATCH_SIZE;
    }

    /**
     * Get the effective maximum batch execution time.
     *
     * @since 2.6.0
     * @return int
     */
    public static function maxBatchExecutionTime(): int {
        /**
         * Filter the maximum batch execution time in seconds.
         *
         * @since 2.6.0
         * @param int $seconds Default maximum execution time.
         */
        $value = (int) apply_filters( 'aipt_automation_max_batch_execution_time', self::MAX_BATCH_EXECUTION_TIME );
        return $value >= 1 ? $value : self::MAX_BATCH_EXECUTION_TIME;
    }

    /**
     * Get the effective tick time budget.
     *
     * @since 2.6.0
     * @return int
     */
    public static function tickTimeBudget(): int {
        /**
         * Filter the tick time budget in seconds.
         *
         * @since 2.6.0
         * @param int $seconds Default tick time budget.
         */
        $value = (int) apply_filters( 'aipt_automation_tick_time_budget', self::TICK_TIME_BUDGET );
        return $value >= 1 ? $value : self::TICK_TIME_BUDGET;
    }

    /**
     * Get the effective window size.
     *
     * @since 2.6.0
     * @return int
     */
    public static function windowSize(): int {
        /**
         * Filter the default window size for claimed items per tick.
         *
         * @since 2.6.0
         * @param int $size Default window size.
         */
        $value = (int) apply_filters( 'aipt_automation_window_size', self::WINDOW_SIZE );

        if ( $value < self::MIN_WINDOW_SIZE || $value > self::MAX_WINDOW_SIZE ) {
            return self::WINDOW_SIZE;
        }
        return $value;
    }

    /**
     * Get the effective reschedule delay.
     *
     * @since 2.6.0
     * @return int
     */
    public static function rescheduleDelay(): int {
        /**
         * Filter the reschedule delay in seconds.
         *
         * @since 2.6.0
         * @param int $seconds Default reschedule delay.
         */
        $value = (int) apply_filters( 'aipt_automation_reschedule_delay', self::RESCHEDULE_DELAY );
        return $value >= 1 ? $value : self::RESCHEDULE_DELAY;
    }

    /**
     * Get the effective lock TTL.
     *
     * @since 2.6.0
     * @return int
     */
    public static function lockTtl(): int {
        /**
         * Filter the per-job execution lock TTL in seconds.
         *
         * @since 2.6.0
         * @param int $seconds Default lock TTL.
         */
        $value = (int) apply_filters( 'aipt_automation_lock_ttl', self::LOCK_TTL );

        return $value >= 10 ? $value : self::LOCK_TTL;
    }

    /**
     * Get the effective immediate execution delay.
     *
     * @since 2.6.0
     * @return int
     */
    public static function immediateExecutionDelay(): int {
        /**
         * Filter the immediate execution delay in seconds.
         *
         * @since 2.6.0
         * @param int $seconds Default immediate execution delay.
         */
        $value = (int) apply_filters( 'aipt_automation_immediate_execution_delay', self::IMMEDIATE_EXECUTION_DELAY );
        return $value >= 1 ? $value : self::IMMEDIATE_EXECUTION_DELAY;
    }
}
