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, "导出功能尚未实现"));
        }

        /// <summary>
        /// 机床维度统计卡片
        /// GET /api/admin/production/machine/summary
        /// </summary>
        [HttpGet]
        [Route("machine/summary")]
        public IHttpActionResult GetMachineSummary(DateTime? startDate = null, DateTime? endDate = null, int? workshopId = null)
        {
            var result = _productionService.GetMachineSummary(startDate, endDate, workshopId);
            return Ok(ApiResponse<MachineProductionSummaryResponse>.Success(result));
        }

        /// <summary>
        /// 机床维度明细列表
        /// GET /api/admin/production/machine/list
        /// </summary>
        [HttpGet]
        [Route("machine/list")]
        public IHttpActionResult GetMachineList(DateTime? startDate = null, DateTime? endDate = null, int? workshopId = null, int? machineId = null)
        {
            var result = _productionService.GetMachineList(startDate, endDate, workshopId, machineId);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 员工维度统计卡片
        /// GET /api/admin/production/worker/summary
        /// </summary>
        [HttpGet]
        [Route("worker/summary")]
        public IHttpActionResult GetWorkerSummary(DateTime? startDate = null, DateTime? endDate = null)
        {
            var result = _productionService.GetWorkerSummary(startDate, endDate);
            return Ok(ApiResponse<WorkerProductionSummaryResponse>.Success(result));
        }

        /// <summary>
        /// 员工维度明细列表
        /// GET /api/admin/production/worker/list
        /// </summary>
        [HttpGet]
        [Route("worker/list")]
        public IHttpActionResult GetWorkerList(DateTime? startDate = null, DateTime? endDate = null, int? workerId = null)
        {
            var result = _productionService.GetWorkerList(startDate, endDate, workerId);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 程序维度统计卡片
        /// GET /api/admin/production/program/summary
        /// </summary>
        [HttpGet]
        [Route("program/summary")]
        public IHttpActionResult GetProgramSummary(DateTime? startDate = null, DateTime? endDate = null, int? workshopId = null)
        {
            var result = _productionService.GetProgramSummary(startDate, endDate, workshopId);
            return Ok(ApiResponse<ProgramProductionSummaryResponse>.Success(result));
        }

        /// <summary>
        /// 程序维度明细列表
        /// GET /api/admin/production/program/list
        /// </summary>
        [HttpGet]
        [Route("program/list")]
        public IHttpActionResult GetProgramList(DateTime? startDate = null, DateTime? endDate = null, string programName = null)
        {
            var result = _productionService.GetProgramList(startDate, endDate, programName);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }
    }
}
