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

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 员工管理控制器
    /// </summary>
    [RoutePrefix("api/admin/worker")]
    [JwtAuthFilter]
    public class WorkerController : ApiController
    {
        private readonly IWorkerService _workerService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public WorkerController(IWorkerService workerService)
        {
            _workerService = workerService;
        }

        /// <summary>
        /// 工人列表（分页）
        /// GET /api/admin/worker
        /// </summary>
        [HttpGet]
        [Route("")]
        public IHttpActionResult GetList([FromUri] WorkerQuery query)
        {
            if (query == null) query = new WorkerQuery();
            var result = _workerService.GetList(query);
            return Ok(ApiResponse<PagedResult<WorkerListItem>>.Success(result));
        }

        /// <summary>
        /// 工人详情
        /// GET /api/admin/worker/{id}
        /// </summary>
        [HttpGet]
        [Route("{id:int}")]
        public IHttpActionResult GetById(int id)
        {
            var result = _workerService.GetById(id);
            return Ok(ApiResponse<WorkerDetailResponse>.Success(result));
        }

        /// <summary>
        /// 新增工人
        /// POST /api/admin/worker
        /// </summary>
        [HttpPost]
        [Route("")]
        public IHttpActionResult Create([FromBody] CreateWorkerRequest request)
        {
            var id = _workerService.Create(request);
            return Ok(ApiResponse<object>.Success(new { id }));
        }

        /// <summary>
        /// 编辑工人
        /// PUT /api/admin/worker/{id}
        /// </summary>
        [HttpPut]
        [Route("{id:int}")]
        public IHttpActionResult Update(int id, [FromBody] UpdateWorkerRequest request)
        {
            var result = _workerService.Update(id, request);
            return Ok(ApiResponse<object>.Success(null));
        }

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

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

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

        /// <summary>
        /// 绑定机床
        /// POST /api/admin/worker/{id}/bind
        /// </summary>
        [HttpPost]
        [Route("{id:int}/bind")]
        public IHttpActionResult BindMachine(int id, [FromBody] BindMachineRequest request)
        {
            var result = _workerService.BindMachine(id, request.MachineId);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 解绑机床
        /// POST /api/admin/worker/{id}/unbind
        /// </summary>
        [HttpPost]
        [Route("{id:int}/unbind")]
        public IHttpActionResult UnbindMachine(int id, [FromBody] BindMachineRequest request)
        {
            var result = _workerService.UnbindMachine(id, request.MachineId);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 绑定机床列表
        /// GET /api/admin/worker/{id}/machines
        /// </summary>
        [HttpGet]
        [Route("{id:int}/machines")]
        public IHttpActionResult GetMachines(int id)
        {
            var result = _workerService.GetMachines(id);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

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

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

        /// <summary>
        /// 可绑定机床列表（未绑定其他工人的）
        /// GET /api/admin/worker/available-machines
        /// </summary>
        [HttpGet]
        [Route("available-machines")]
        public IHttpActionResult GetAvailableMachines()
        {
            var result = _workerService.GetAvailableMachines();
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }
    }

    /// <summary>
    /// 绑定/解绑机床请求
    /// </summary>
    public class BindMachineRequest
    {
        /// <summary>机床ID</summary>
        public int MachineId { get; set; }
    }
}
