using System;
using System.Web.Http;
using CncModels.Dto;
using CncModels.Dto.Production;
using CncService.Interface;
using CncWebApi.Infrastructure;

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 产量报表控制器
    /// </summary>
    [RoutePrefix("api/admin/production")]
    [JwtAuthFilter]
    public class ProductionController : ApiController
    {
        private readonly IProductionService _productionService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ProductionController(IProductionService productionService)
        {
            _productionService = productionService;
        }

        /// <summary>
        /// 日产量列表（分页）
        /// GET /api/admin/production/daily
        /// </summary>
        [HttpGet]
        [Route("daily")]
        public IHttpActionResult GetList([FromUri] ProductionQuery query)
        {
            if (query == null) query = new ProductionQuery();
            var result = _productionService.GetList(query);
            return Ok(ApiResponse<PagedResult<DailyProductionListItem>>.Success(result));
        }

        /// <summary>
        /// 日汇总统计
        /// GET /api/admin/production/daily-summary
        /// </summary>
        [HttpGet]
        [Route("daily-summary")]
        public IHttpActionResult GetSummary(DateTime? startDate = null, DateTime? endDate = null, int? workshopId = null, int? machineId = null, int? workerId = null)
        {
            var result = _productionService.GetSummary(startDate, endDate, workshopId, machineId, workerId);
            return Ok(ApiResponse<DailySummaryResponse>.Success(result));
        }

        /// <summary>
        /// 修正产量
        /// POST /api/admin/production/adjust
        /// </summary>
        [HttpPost]
        [Route("adjust")]
        public IHttpActionResult Adjust([FromBody] ProductionAdjustRequest request)
        {
            var result = _productionService.Adjust(request);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 修正历史
        /// GET /api/admin/production/{recordId}/adjustment-history
        /// </summary>
        [HttpGet]
        [Route("{recordId:int}/adjustment-history")]
        public IHttpActionResult GetAdjustmentHistory(int recordId)
        {
            var result = _productionService.GetAdjustmentHistory(recordId);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 导出报表（占位，暂返回空结果）
        /// GET /api/admin/production/export
        /// </summary>
        [HttpGet]
        [Route("export")]
        public IHttpActionResult Export(DateTime? startDate = null, DateTime? endDate = null, int? workshopId = null)
        {
            // 导出功能暂不实现，返回提示
            return Ok(ApiResponse<object>.Fail(40001, "导出功能尚未实现"));
        }
    }
}
