using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using CncModels.Dto;
using CncModels.Dto.Common;
using CncService.Interface;
using CncWebApi.Infrastructure;

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 公共下拉选项控制器
    /// </summary>
    [RoutePrefix("api/admin")]
    [JwtAuthFilter]
    public class OptionController : ApiController
    {
        private readonly IWorkshopService _workshopService;
        private readonly IBrandService _brandService;
        private readonly IMachineService _machineService;
        private readonly IWorkerService _workerService;
        private readonly ICollectAddressService _collectAddressService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public OptionController(
            IWorkshopService workshopService,
            IBrandService brandService,
            IMachineService machineService,
            IWorkerService workerService,
            ICollectAddressService collectAddressService)
        {
            _workshopService = workshopService;
            _brandService = brandService;
            _machineService = machineService;
            _workerService = workerService;
            _collectAddressService = collectAddressService;
        }

        /// <summary>
        /// 车间下拉
        /// GET /api/admin/workshop/list
        /// </summary>
        [HttpGet]
        [Route("workshop/list")]
        public IHttpActionResult WorkshopList()
        {
            var list = _workshopService.GetList(null);
            var items = list.Select(w => new SimpleOption { Value = w.Id.ToString(), Label = w.Name }).ToList();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items }));
        }

        /// <summary>
        /// 品牌下拉
        /// GET /api/admin/brand/list
        /// </summary>
        [HttpGet]
        [Route("brand/list")]
        public IHttpActionResult BrandList()
        {
            var list = _brandService.GetList();
            var items = list.Select(b => new SimpleOption { Value = b.Id.ToString(), Label = b.BrandName }).ToList();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items }));
        }

        /// <summary>
        /// 机床下拉
        /// GET /api/admin/machine/list
        /// </summary>
        [HttpGet]
        [Route("machine/list")]
        public IHttpActionResult MachineList()
        {
            var query = new CncModels.Dto.Machine.MachineQuery { Page = 1, PageSize = 1000 };
            var paged = _machineService.GetList(query);
            var items = paged.Items.Select(m => new SimpleOption { Value = m.Id.ToString(), Label = m.Name ?? m.DeviceCode }).ToList();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items }));
        }

        /// <summary>
        /// 工人下拉
        /// GET /api/admin/worker/list
        /// </summary>
        [HttpGet]
        [Route("worker/list")]
        public IHttpActionResult WorkerList()
        {
            var query = new CncModels.Dto.Worker.WorkerQuery { Page = 1, PageSize = 1000 };
            var paged = _workerService.GetList(query);
            var items = paged.Items.Select(w => new SimpleOption { Value = w.Id.ToString(), Label = w.Name + "(" + w.Code + ")" }).ToList();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items }));
        }

        /// <summary>
        /// 采集地址下拉
        /// GET /api/admin/collect-address/list
        /// </summary>
        [HttpGet]
        [Route("collect-address/list")]
        public IHttpActionResult CollectAddressList()
        {
            var query = new CncModels.Dto.CollectAddress.CollectAddressQuery { Page = 1, PageSize = 1000 };
            var paged = _collectAddressService.GetList(query);
            var items = paged.Items.Select(a => new SimpleOption { Value = a.Id.ToString(), Label = a.Name }).ToList();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items }));
        }
    }
}
