using System;
using System.Collections.Generic;
using CncService.Interface;
using CncModels.Dto.Screen;
using CncModels.Entity;
using CncModels.Constants;
using CncRepository.Interface;

namespace CncService.Impl
{
    /// <summary>
    /// 大屏配置实现
    /// </summary>
    public class ScreenService : IScreenService
    {
        private readonly IScreenConfigRepository _screenConfigRepository;
        private readonly IScreenFilterRepository _screenFilterRepository;
        private readonly IWorkshopRepository _workshopRepository;

        public ScreenService(
            IScreenConfigRepository screenConfigRepository,
            IScreenFilterRepository screenFilterRepository,
            IWorkshopRepository workshopRepository)
        {
            _screenConfigRepository = screenConfigRepository ?? throw new ArgumentNullException(nameof(screenConfigRepository));
            _screenFilterRepository = screenFilterRepository ?? throw new ArgumentNullException(nameof(screenFilterRepository));
            _workshopRepository = workshopRepository ?? throw new ArgumentNullException(nameof(workshopRepository));
        }

        /// <inheritdoc/>
        public ScreenSummaryResponse GetSummary()
        {
            // 简化实现，返回默认值（实际由Controller调用Dashboard服务获取真实数据）
            return new ScreenSummaryResponse
            {
                MachineCount = 0,
                ProductionToday = 0,
                AlertCount = 0,
                OnlineCount = 0
            };
        }

        /// <inheritdoc/>
        public List<ScreenConfig> GetConfigs()
        {
            return _screenConfigRepository.GetAll();
        }

        /// <inheritdoc/>
        public bool UpdateConfig(ScreenConfig entity)
        {
            if (entity == null) throw new BusinessException(ErrorCode.BadRequest, "配置不能为空");
            return _screenConfigRepository.Update(entity);
        }

        /// <inheritdoc/>
        public List<ScreenFilter> GetFilters(string screenKey)
        {
            if (string.IsNullOrWhiteSpace(screenKey)) throw new BusinessException(ErrorCode.BadRequest, "screenKey不能为空");
            return _screenFilterRepository.GetByScreenKey(screenKey);
        }

        /// <inheritdoc/>
        public int CreateFilter(ScreenFilter entity)
        {
            if (entity == null) throw new BusinessException(ErrorCode.BadRequest, "筛选项不能为空");
            return _screenFilterRepository.Create(entity);
        }

        /// <inheritdoc/>
        public bool UpdateFilter(ScreenFilter entity)
        {
            if (entity == null) throw new BusinessException(ErrorCode.BadRequest, "筛选项不能为空");
            return _screenFilterRepository.Update(entity);
        }

        /// <inheritdoc/>
        public bool DeleteFilter(int id)
        {
            if (id <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的筛选ID");
            return _screenFilterRepository.Delete(id);
        }
    }
}
