using System.Threading.Tasks;
using BLL.Interfaces;
using Microsoft.AspNetCore.Mvc;
using MDL.DTOs;

namespace CONTROLLER.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TagModuleController : ControllerBase
    {
        private readonly ITagTemplateService _tagTemplateService;
        private readonly ITagInstanceService _tagInstanceService;
        private readonly ITagTriggerService _tagTriggerService;
        private readonly ITagGenerationService _tagGenerationService;

        public TagModuleController(
            ITagTemplateService tagTemplateService,
            ITagInstanceService tagInstanceService,
            ITagTriggerService tagTriggerService,
            ITagGenerationService tagGenerationService)
        {
            _tagTemplateService = tagTemplateService;
            _tagInstanceService = tagInstanceService;
            _tagTriggerService = tagTriggerService;
            _tagGenerationService = tagGenerationService;
        }

        // 标签模板管理接口
        [HttpPost("template")]
        public async Task<IActionResult> CreateTemplate([FromBody] CreateTagTemplateDTO dto)
        {
            var result = await _tagTemplateService.CreateTemplateAsync(dto);
            return Ok(result);
        }

        [HttpPut("template/{id}")]
        public async Task<IActionResult> UpdateTemplate(int id, [FromBody] UpdateTagTemplateDTO dto)
        {
            var result = await _tagTemplateService.UpdateTemplateAsync(id, dto);
            return Ok(result);
        }

        [HttpDelete("template/{id}")]
        public async Task<IActionResult> DeleteTemplate(int id)
        {
            var result = await _tagTemplateService.DeleteTemplateAsync(id);
            return Ok(result);
        }

        [HttpGet("template/{id}")]
        public async Task<IActionResult> GetTemplate(int id)
        {
            var result = await _tagTemplateService.GetTemplateAsync(id);
            return Ok(result);
        }

        [HttpGet("template/type/{tagType}")]
        public async Task<IActionResult> GetTemplateByType(string tagType)
        {
            var result = await _tagTemplateService.GetTemplateByTypeAsync(tagType);
            return Ok(result);
        }

        [HttpGet("templates")]
        public async Task<IActionResult> GetAllTemplates()
        {
            var result = await _tagTemplateService.GetAllTemplatesAsync();
            return Ok(result);
        }

        // 标签实例管理接口
        [HttpPost("instance")]
        public async Task<IActionResult> CreateInstance([FromBody] CreateTagInstanceDTO dto)
        {
            var result = await _tagInstanceService.CreateInstanceAsync(dto);
            return Ok(result);
        }

        [HttpGet("instance/{id}")]
        public async Task<IActionResult> GetInstance(long id)
        {
            var result = await _tagInstanceService.GetInstanceAsync(id);
            return Ok(result);
        }

        [HttpGet("instances/waybill/{waybillNumber}")]
        public async Task<IActionResult> GetInstancesByWaybill(string waybillNumber)
        {
            var result = await _tagInstanceService.GetInstancesByWaybillAsync(waybillNumber);
            return Ok(result);
        }

        [HttpGet("instances/type/{tagType}")]
        public async Task<IActionResult> GetInstancesByType(string tagType)
        {
            var result = await _tagInstanceService.GetInstancesByTypeAsync(tagType);
            return Ok(result);
        }

        [HttpPut("instance/{id}/status")]
        public async Task<IActionResult> UpdateInstanceStatus(long id, [FromBody] string status)
        {
            var result = await _tagInstanceService.UpdateInstanceStatusAsync(id, status);
            return Ok(result);
        }

        // 触发检查接口
        [HttpPost("trigger/check")]
        public async Task<IActionResult> CheckTrigger([FromBody] TriggerCheckDTO dto)
        {
            var result = await _tagTriggerService.CheckTriggerAsync(dto);
            return Ok(result);
        }

        [HttpPost("trigger")]
        public async Task<IActionResult> TriggerTag([FromBody] CreateTagInstanceDTO dto)
        {
            var result = await _tagTriggerService.TriggerTagAsync(dto);
            return Ok(result);
        }

        // 标签生成接口
        [HttpPost("generate")]
        public async Task<IActionResult> GenerateTag([FromBody] GenerateTagDTO dto)
        {
            var result = await _tagGenerationService.GenerateTagAsync(dto);
            return Ok(result);
        }

        // 标签渲染接口
        [HttpPost("render")]
        public async Task<IActionResult> RenderTag([FromBody] RenderTagDTO dto)
        {
            var result = await _tagGenerationService.RenderTagAsync(dto);
            return Ok(result);
        }

        // 条形码生成接口
        [HttpPost("barcode")]
        public async Task<IActionResult> GenerateBarcode([FromBody] string content)
        {
            var result = await _tagGenerationService.GenerateBarcodeAsync(content);
            return Ok(result);
        }
    }
}
