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

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 采集地址控制器
    /// </summary>
    [RoutePrefix("api/admin/collect-address")]
    [JwtAuthFilter]
    public class CollectAddressController : ApiController
    {
        private readonly ICollectAddressService _collectAddressService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public CollectAddressController(ICollectAddressService collectAddressService)
        {
            _collectAddressService = collectAddressService;
        }

        /// <summary>
        /// 地址列表（分页）
        /// GET /api/admin/collect-address
        /// </summary>
        [HttpGet]
        [Route("")]
        public IHttpActionResult GetList([FromUri] CollectAddressQuery query)
        {
            if (query == null) query = new CollectAddressQuery();
            var result = _collectAddressService.GetList(query);
            return Ok(ApiResponse<PagedResult<CollectAddressListItem>>.Success(result));
        }

        /// <summary>
        /// 地址详情
        /// GET /api/admin/collect-address/{id}
        /// </summary>
        [HttpGet]
        [Route("{id:int}")]
        public IHttpActionResult GetById(int id)
        {
            var result = _collectAddressService.GetById(id);
            return Ok(ApiResponse<CollectAddressDetailResponse>.Success(result));
        }

        /// <summary>
        /// 新增地址
        /// POST /api/admin/collect-address
        /// </summary>
        [HttpPost]
        [Route("")]
        public IHttpActionResult Create([FromBody] CreateCollectAddressRequest request)
        {
            var id = _collectAddressService.Create(request);
            return Ok(ApiResponse<object>.Success(new { id }));
        }

        /// <summary>
        /// 编辑地址
        /// PUT /api/admin/collect-address/{id}
        /// </summary>
        [HttpPut]
        [Route("{id:int}")]
        public IHttpActionResult Update(int id, [FromBody] UpdateCollectAddressRequest request)
        {
            var result = _collectAddressService.Update(id, request);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 删除地址
        /// DELETE /api/admin/collect-address/{id}
        /// </summary>
        [HttpDelete]
        [Route("{id:int}")]
        public IHttpActionResult Delete(int id)
        {
            var result = _collectAddressService.Delete(id);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 批量删除采集地址
        /// POST /api/admin/collect-address/batch-delete
        /// </summary>
        [HttpPost]
        [Route("batch-delete")]
        public IHttpActionResult BatchDelete([FromBody] BatchDeleteRequest request)
        {
            var count = _collectAddressService.BatchDelete(request.Ids);
            return Ok(ApiResponse<object>.Success(new { count }));
        }

        /// <summary>
        /// 启停地址
        /// PUT /api/admin/collect-address/{id}/toggle
        /// </summary>
        [HttpPut]
        [Route("{id:int}/toggle")]
        public IHttpActionResult ToggleEnabled(int id)
        {
            var result = _collectAddressService.ToggleEnabled(id);
            return Ok(ApiResponse<object>.Success(null));
        }

        /// <summary>
        /// 地址下关联机床列表
        /// GET /api/admin/collect-address/{id}/machines
        /// </summary>
        [HttpGet]
        [Route("{id:int}/machines")]
        public IHttpActionResult GetMachines(int id)
        {
            var result = _collectAddressService.GetMachines(id);
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

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

        /// <summary>
        /// 地址最新原始JSON
        /// GET /api/admin/collect-address/{id}/raw-json
        /// </summary>
        [HttpGet]
        [Route("{id:int}/raw-json")]
        public IHttpActionResult GetRawJson(int id, string recordId = null)
        {
            var result = _collectAddressService.GetRawJson(id, recordId);
            return Ok(ApiResponse<CollectAddressRawJsonResponse>.Success(result));
        }
    }
}
