using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Text;

namespace TEAMModelOS.SDK.DI
{
    public static class SnowflakeIDExtensions
    {
        /// <summary>
        /// 分散式ID產生器 (Snowflake算法，支援線程安全)
        /// </summary>
        /// <param name="datacenterId">數據中心ID (0~31)</param>
        /// <param name="workerId">工作機器ID (0~31)</param>
        public static IServiceCollection AddSnowflakeId(this IServiceCollection services, long datacenterId, long workerId)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));            
            if (datacenterId > 31 || datacenterId < 0) throw new Exception("datacenterId only 0-31");            
            if (workerId > 31 || workerId < 0) throw new Exception("workerId only 0-31");
            
            services.TryAddSingleton<SnowflakeId>();
            services.Configure<SnowflakeIDOptions>(o => { o.DatacenterId = datacenterId; o.WorkerId = workerId; });

            return services;
        }
    }
}
