using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using CncModels.Dto;
using CncModels.Dto.Alert;
using CncService.Interface;
using CncWebApi.Infrastructure;

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 告警中心控制器
    /// </summary>
    [RoutePrefix("api/admin/alert")]
    [JwtAuthFilter]
    public class AlertController : ApiController
    {
        private readonly IAlertService _alertService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public AlertController(IAlertService alertService)
        {
            _alertService = alertService;
        }

        /// <summary>
        /// 告警列表（分页）
        /// GET /api/admin/alert
        /// </summary>
        [HttpGet]
        [Route("")]
        public IHttpActionResult GetList([FromUri] AlertQuery query)
        {
            if (query == null) query = new AlertQuery();
            var result = _alertService.GetList(query);
            return Ok(ApiResponse<PagedResult<AlertListItem>>.Success(result));
        }

        /// <summary>
        /// 告警统计
        /// GET /api/admin/alert/statistics
        /// </summary>
        [HttpGet]
        [Route("statistics")]
        public IHttpActionResult GetStatistics()
        {
            var result = _alertService.GetStatistics();
            return Ok(ApiResponse<AlertStatisticsResponse>.Success(result));
        }

        /// <summary>
        /// 处理单条告警
        /// PUT /api/admin/alert/{id}/resolve
        /// </summary>
        [HttpPut]
        [Route("{id:long}/resolve")]
        public IHttpActionResult Resolve(long id)
        {
            var result = _alertService.Resolve(id);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 批量处理告警
        /// POST /api/admin/alert/batch-resolve
        /// </summary>
        [HttpPost]
        [Route("batch-resolve")]
        public IHttpActionResult BatchResolve([FromBody] BatchResolveRequest request)
        {
            var ids = request.Ids.Select(i => (long)i).ToList();
            var count = _alertService.BatchResolve(ids);
            return Ok(ApiResponse<object>.Success(new { count }));
        }
    }
}
