using Entities.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TechHelper.Services;

namespace TechHelper.Server.Controllers
{
	[Route("api/note")]
	[ApiController]
	public class NoteController : ControllerBase
	{
		private readonly INoteService _noteService;

		// 通过依赖注入获取 NoteService
		public NoteController(INoteService noteService)
		{
			_noteService = noteService;
		}

		/// <summary>
		/// 获取所有全局数据。
		/// GET: api/Note
		/// </summary>
		[HttpGet]
		public async Task<IActionResult> GetAll([FromQuery] QueryParameter query)
		{
			var response = await _noteService.GetAllAsync(query);
			return Ok(response);
		}

		/// <summary>
		/// 根据 ID 获取单个全局数据。
		/// GET: api/Note/{id}
		/// </summary>
		[HttpGet("{id}")]
		public async Task<IActionResult> Get(byte id)
		{
			var response = await _noteService.GetAsync(id);
			if (!response.Status)
			{
				return NotFound(response);
			}
			return Ok(response);
		}

		/// <summary>
		/// 添加新的全局数据。
		/// POST: api/Note
		/// </summary>
		[HttpPost]
		public async Task<IActionResult> Add([FromBody] GlobalDto model)
		{
			var response = await _noteService.AddAsync(model);
			if (!response.Status)
			{
				return BadRequest(response);
			}
			return Ok(response);
		}

		/// <summary>
		/// 更新已存在的全局数据。
		/// PUT: api/Note
		/// </summary>
		[HttpPut]
		public async Task<IActionResult> Update([FromBody] GlobalDto model)
		{
			var response = await _noteService.UpdateAsync(model);
			if (!response.Status)
			{
				return NotFound(response);
			}
			return Ok(response);
		}

		/// <summary>
		/// 根据 ID 删除全局数据。
		/// DELETE: api/Note/{id}
		/// </summary>
		[HttpDelete("{id}")]
		public async Task<IActionResult> Delete(byte id)
		{
			var response = await _noteService.DeleteAsync(id);
			if (!response.Status)
			{
				return NotFound(response);
			}
			return Ok(response);
		}
	}
}
