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

namespace CncWebApi.Controllers
{
    /// <summary>
    /// 品牌模板控制器
    /// </summary>
    [RoutePrefix("api/admin/brand")]
    [JwtAuthFilter]
    public class BrandController : ApiController
    {
        private readonly IBrandService _brandService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public BrandController(IBrandService brandService)
        {
            _brandService = brandService;
        }

        /// <summary>
        /// 品牌列表
        /// GET /api/admin/brand
        /// </summary>
        [HttpGet]
        [Route("")]
        public IHttpActionResult GetList()
        {
            var result = _brandService.GetList();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }

        /// <summary>
        /// 品牌详情（含字段映射）
        /// GET /api/admin/brand/{id}
        /// </summary>
        [HttpGet]
        [Route("{id:int}")]
        public IHttpActionResult GetById(int id)
        {
            var result = _brandService.GetById(id);
            return Ok(ApiResponse<BrandDetailResponse>.Success(result));
        }

        /// <summary>
        /// 新增品牌
        /// POST /api/admin/brand
        /// </summary>
        [HttpPost]
        [Route("")]
        public IHttpActionResult Create([FromBody] CreateBrandRequest request)
        {
            var id = _brandService.Create(request);
            return Ok(ApiResponse<object>.Success(new { id }));
        }

        /// <summary>
        /// 编辑品牌
        /// PUT /api/admin/brand/{id}
        /// </summary>
        [HttpPut]
        [Route("{id:int}")]
        public IHttpActionResult Update(int id, [FromBody] UpdateBrandRequest request)
        {
            var result = _brandService.Update(id, request);
            return Ok(ApiResponse<object>.Success(null));
        }

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

        /// <summary>
        /// 复制品牌
        /// POST /api/admin/brand/{id}/copy
        /// </summary>
        [HttpPost]
        [Route("{id:int}/copy")]
        public IHttpActionResult Copy(int id)
        {
            var newId = _brandService.Copy(id);
            return Ok(ApiResponse<object>.Success(new { id = newId }));
        }

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

        /// <summary>
        /// 标准字段列表
        /// GET /api/admin/brand/standard-fields
        /// </summary>
        [HttpGet]
        [Route("standard-fields")]
        public IHttpActionResult GetStandardFields()
        {
            var result = _brandService.GetStandardFields();
            // Mock约定：data: { items: [...] }
            return Ok(ApiResponse<object>.Success(new { items = result }));
        }
    }
}
