using Entities.Contracts;
using Entities.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Net;
using System.Security.Claims;
using TechHelper.Services.Beta;
using TechHelper.Services;

namespace TechHelper.Server.Controllers
{
    /// <summary>
    /// 题型管理控制器
    /// 处理题型相关的操作，如创建、更新、删除题型等
    /// </summary>
    [Route("api/question-type")]
    [ApiController]
    public class QuestionTypeController : ControllerBase
    {
        private readonly IQuestionTypeService _questionTypeService;
        private readonly UserManager<User> _userManager;

        /// <summary>
        /// 初始化题型控制器
        /// </summary>
        /// <param name="questionTypeService">题型服务</param>
        /// <param name="userManager">用户管理服务</param>
        public QuestionTypeController(IQuestionTypeService questionTypeService, UserManager<User> userManager)
        {
            _questionTypeService = questionTypeService;
            _userManager = userManager;
        }

        #region 题型基本信息CRUD操作

        /// <summary>
        /// 获取题型列表（支持搜索）
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <returns>题型列表</returns>
        /// <response code="200">成功获取题型列表</response>
        /// <response code="400">获取失败</response>
        [HttpGet]
        [Authorize(Roles = "Teacher,Admin")]
        public async Task<IActionResult> GetAll([FromQuery] QueryParameter query)
        {
            var result = await _questionTypeService.GetAllAsync(query);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 根据ID获取题型详细信息
        /// </summary>
        /// <param name="id">题型ID</param>
        /// <returns>题型详细信息</returns>
        /// <response code="200">成功获取题型信息</response>
        /// <response code="400">题型未找到或获取失败</response>
        [HttpGet("{id}")]
        [Authorize(Roles = "Teacher,Admin")]
        public async Task<IActionResult> GetById(Guid id)
        {
            var result = await _questionTypeService.GetAsync(id);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 创建新题型
        /// </summary>
        /// <param name="model">题型数据传输对象</param>
        /// <returns>创建结果</returns>
        /// <response code="200">创建成功</response>
        /// <response code="400">创建失败</response>
        [HttpPost]
        [Authorize(Roles = "Admin, Teacher")]
        public async Task<IActionResult> Create([FromBody] QuestionTypeDto model)
        {
            var result = await _questionTypeService.AddAsync(model);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 更新题型信息
        /// </summary>
        /// <param name="model">题型数据传输对象</param>
        /// <returns>更新结果</returns>
        /// <response code="200">更新成功</response>
        /// <response code="400">更新失败</response>
        [HttpPut]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Update([FromBody] QuestionTypeDto model)
        {
            var result = await _questionTypeService.UpdateAsync(model);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 删除题型
        /// </summary>
        /// <param name="id">题型ID</param>
        /// <returns>删除结果</returns>
        /// <response code="200">删除成功</response>
        /// <response code="400">删除失败</response>
        [HttpDelete("{id}")]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var result = await _questionTypeService.DeleteAsync(id);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Message);
        }

        #endregion
    }
}
