using System;
using System.Threading;

namespace AdvChkSys.Threading
{
    /// <summary>
    /// Provides centralized configuration for threading in the chunk system.
    /// </summary>
    public static class ChunkThreadingConfiguration
    {
        private static int _defaultMaxDegreeOfParallelism = Environment.ProcessorCount;
        private static int _chunkOperationTimeout = 30000; // 30 seconds
        private static int _lockCleanupInterval = 10; // 10 minutes

        /// <summary>
        /// Gets or sets the default maximum degree of parallelism for chunk operations.
        /// </summary>
        public static int DefaultMaxDegreeOfParallelism
        {
            get => _defaultMaxDegreeOfParallelism;
            set
            {
                if (value < 1)
                    throw new ArgumentOutOfRangeException(nameof(value), "Parallelism must be at least 1");
                _defaultMaxDegreeOfParallelism = value;
                ChunkTaskScheduler.MaxDegreeOfParallelism = value;
            }
        }

        /// <summary>
        /// Gets or sets the timeout in milliseconds for chunk operations.
        /// </summary>
        public static int ChunkOperationTimeoutMs
        {
            get => _chunkOperationTimeout;
            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException(nameof(value), "Timeout cannot be negative");
                _chunkOperationTimeout = value;
            }
        }

        /// <summary>
        /// Gets or sets the interval in minutes for cleaning up unused locks.
        /// </summary>
        public static int LockCleanupIntervalMinutes
        {
            get => _lockCleanupInterval;
            set
            {
                if (value < 1)
                    throw new ArgumentOutOfRangeException(nameof(value), "Cleanup interval must be at least 1 minute");
                _lockCleanupInterval = value;
            }
        }

        /// <summary>
        /// Creates a cancellation token with the default timeout.
        /// </summary>
        public static CancellationToken CreateTimeoutToken(CancellationToken cancellationToken = default)
        {
            return ChunkTaskScheduler.CreateTimeoutToken(
                TimeSpan.FromMilliseconds(_chunkOperationTimeout),
                cancellationToken);
        }

        /// <summary>
        /// Configures the system for high throughput (more parallelism, longer timeouts).
        /// </summary>
        public static void ConfigureForHighThroughput()
        {
            DefaultMaxDegreeOfParallelism = Math.Max(4, Environment.ProcessorCount * 2);
            ChunkOperationTimeoutMs = 60000; // 1 minute
            LockCleanupIntervalMinutes = 30;
        }

        /// <summary>
        /// Configures the system for low latency (less parallelism, shorter timeouts).
        /// </summary>
        public static void ConfigureForLowLatency()
        {
            DefaultMaxDegreeOfParallelism = Math.Max(2, Environment.ProcessorCount / 2);
            ChunkOperationTimeoutMs = 15000; // 15 seconds
            LockCleanupIntervalMinutes = 5;
        }

        /// <summary>
        /// Configures the system for memory efficiency (less parallelism, more aggressive cleanup).
        /// </summary>
        public static void ConfigureForMemoryEfficiency()
        {
            DefaultMaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount / 4);
            ChunkOperationTimeoutMs = 45000; // 45 seconds
            LockCleanupIntervalMinutes = 2;
        }
    }
}