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

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 设备管理控制器
    /// </summary>
    [RoutePrefix("api/admin/machine")]
    [JwtAuthFilter]
    public class MachineController : ApiController
    {
        private readonly IMachineService _machineService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MachineController(IMachineService machineService)
        {
            _machineService = machineService;
        }

        /// <summary>
        /// 机床列表（分页）
        /// GET /api/admin/machine
        /// </summary>
        [HttpGet]
        [Route("")]
        public IHttpActionResult GetList([FromUri] MachineQuery query)
        {
            if (query == null) query = new MachineQuery();
            var result = _machineService.GetList(query);
            return Ok(ApiResponse<PagedResult<MachineListItem>>.Success(result));
        }

        /// <summary>
        /// 机床详情
        /// GET /api/admin/machine/{id}
        /// </summary>
        [HttpGet]
        [Route("{id:int}")]
        public IHttpActionResult GetById(int id)
        {
            var result = _machineService.GetById(id);
            return Ok(ApiResponse<MachineDetailResponse>.Success(result));
        }

        /// <summary>
        /// 新增机床
        /// POST /api/admin/machine
        /// </summary>
        [HttpPost]
        [Route("")]
        public IHttpActionResult Create([FromBody] CreateMachineRequest request)
        {
            var id = _machineService.Create(request);
            return Ok(ApiResponse<object>.Success(new { id }));
        }

        /// <summary>
        /// 编辑机床
        /// PUT /api/admin/machine/{id}
        /// </summary>
        [HttpPut]
        [Route("{id:int}")]
        public IHttpActionResult Update(int id, [FromBody] UpdateMachineRequest request)
        {
            var result = _machineService.Update(id, request);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 删除机床
        /// DELETE /api/admin/machine/{id}
        /// </summary>
        [HttpDelete]
        [Route("{id:int}")]
        public IHttpActionResult Delete(int id)
        {
            var result = _machineService.Delete(id);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 批量删除机床
        /// POST /api/admin/machine/batch-delete
        /// </summary>
        [HttpPost]
        [Route("batch-delete")]
        public IHttpActionResult BatchDelete([FromBody] BatchDeleteRequest request)
        {
            var count = _machineService.BatchDelete(request.Ids);
            return Ok(ApiResponse<object>.Success(new { count }));
        }

        /// <summary>
        /// 启停机床
        /// PUT /api/admin/machine/{id}/toggle
        /// </summary>
        [HttpPut]
        [Route("{id:int}/toggle")]
        public IHttpActionResult ToggleEnabled(int id)
        {
            var result = _machineService.ToggleEnabled(id);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 机床实时采集状态
        /// GET /api/admin/machine/{id}/status
        /// </summary>
        [HttpGet]
        [Route("{id:int}/status")]
        public IHttpActionResult GetStatus(int id)
        {
            var result = _machineService.GetStatus(id);
            return Ok(ApiResponse<MachineStatusResponse>.Success(result));
        }

        /// <summary>
        /// 机床今日产量明细
        /// GET /api/admin/machine/{id}/production/today
        /// </summary>
        [HttpGet]
        [Route("{id:int}/production/today")]
        public IHttpActionResult GetTodayProduction(int id)
        {
            var result = _machineService.GetTodayProduction(id);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 机床7天产量趋势
        /// GET /api/admin/machine/{id}/production/trend
        /// </summary>
        [HttpGet]
        [Route("{id:int}/production/trend")]
        public IHttpActionResult GetProductionTrend(int id)
        {
            var result = _machineService.GetProductionTrend(id);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 机床近期采集记录
        /// GET /api/admin/machine/{id}/collect-records
        /// </summary>
        [HttpGet]
        [Route("{id:int}/collect-records")]
        public IHttpActionResult GetCollectRecords(int id)
        {
            var result = _machineService.GetCollectRecords(id);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }
    }
}
