using System;
using System.Collections.Generic;
using System.Web.Http;
using CncModels.Dto;
using CncModels.Dto.CollectLog;
using CncModels.Entity;
using CncService.Interface;
using CncRepository.Interface;
using CncWebApi.Infrastructure;
using System.Web.Http.Description;

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 采集日志管理控制器
    /// </summary>
    [RoutePrefix("api/admin/collect-log")]
    [JwtAuthFilter]
    public class CollectLogController : ApiController
    {
        private readonly ICollectLogService _collectLogService;
        private readonly ICollectRawRepository _rawRepository;

        public CollectLogController(ICollectLogService collectLogService, ICollectRawRepository rawRepository)
        {
            _collectLogService = collectLogService ?? throw new ArgumentNullException(nameof(collectLogService));
            _rawRepository = rawRepository ?? throw new ArgumentNullException(nameof(rawRepository));
        }

        /// <summary>分页查询采集分析记录</summary>
        [HttpGet]
        [Route("analysis")]
        [ResponseType(typeof(ApiResponse<PagedResult<CollectAnalysisListItem>>))]
        public IHttpActionResult GetAnalysisList([FromUri] CollectAnalysisQuery query)
        {
            if (query == null) query = new CollectAnalysisQuery();
            var result = _collectLogService.GetAnalysisList(query);
            return Ok(ApiResponse<PagedResult<CollectAnalysisListItem>>.Success(result));
        }

        /// <summary>获取采集分析详情</summary>
        [HttpGet]
        [Route("analysis/{id:long}")]
        [ResponseType(typeof(ApiResponse<CollectAnalysisDetail>))]
        public IHttpActionResult GetAnalysisDetail(long id)
        {
            var detail = _collectLogService.GetAnalysisDetail(id);
            return Ok(ApiResponse<CollectAnalysisDetail>.Success(detail));
        }

        /// <summary>根据原始日志ID查询关联的分析记录</summary>
        [HttpGet]
        [Route("analysis/by-raw/{rawLogId:long}")]
        [ResponseType(typeof(ApiResponse<List<CollectAnalysisListItem>>))]
        public IHttpActionResult GetAnalysisByRawLogId(long rawLogId)
        {
            var list = _collectLogService.GetAnalysisByRawLogId(rawLogId);
            return Ok(ApiResponse<List<CollectAnalysisListItem>>.Success(list));
        }

        /// <summary>分页查询采集周期</summary>
        [HttpGet]
        [Route("cycle")]
        [ResponseType(typeof(ApiResponse<PagedResult<CollectCycleListItem>>))]
        public IHttpActionResult GetCycleList([FromUri] CollectCycleQuery query)
        {
            if (query == null) query = new CollectCycleQuery();
            var result = _collectLogService.GetCycleList(query);
            return Ok(ApiResponse<PagedResult<CollectCycleListItem>>.Success(result));
        }

        /// <summary>查询原始采集日志</summary>
        [HttpGet]
        [Route("raw")]
        [ResponseType(typeof(ApiResponse<PagedResult<CollectRaw>>))]
        public IHttpActionResult GetRawList([FromUri] int? collectAddressId = null, [FromUri] int page = 1, [FromUri] int pageSize = 20)
        {
            var result = collectAddressId.HasValue
                ? _rawRepository.GetByAddressId(collectAddressId.Value, page, pageSize)
                : _rawRepository.GetAll(page, pageSize);
            return Ok(ApiResponse<PagedResult<CollectRaw>>.Success(result));
        }

        /// <summary>获取原始采集日志详情</summary>
        [HttpGet]
        [Route("raw/{id:long}")]
        [ResponseType(typeof(ApiResponse<CollectRaw>))]
        public IHttpActionResult GetRawDetail(long id)
        {
            var entity = _rawRepository.GetById(id);
            if (entity == null) return NotFound();
            return Ok(ApiResponse<CollectRaw>.Success(entity));
        }
    }
}
