using Entities.Contracts;
using Entities.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using TechHelper.Services;
using TechHelper.Services.Beta;

namespace TechHelper.Server.Controllers
{
    /// <summary>
    /// 题目管理控制器
    /// 处理题目相关的操作，如CRUD、按标题查找等
    /// </summary>
    [Route("api/question")]
    [ApiController]
    public class QuestionController : ControllerBase
    {
        private readonly IQuestionService _questionService;
        private readonly UserManager<User> _userManager;

        /// <summary>
        /// 初始化题目控制器
        /// </summary>
        /// <param name="questionService">题目服务</param>
        /// <param name="userManager">用户管理服务</param>
        public QuestionController(IQuestionService questionService, UserManager<User> userManager)
        {
            _questionService = questionService;
            _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 _questionService.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 _questionService.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 = "Teacher,Admin")]
        public async Task<IActionResult> Create([FromBody] QuestionDto model)
        {
            var result = await _questionService.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 = "Teacher,Admin")]
        public async Task<IActionResult> Update([FromBody] QuestionDto model)
        {
            var result = await _questionService.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 _questionService.DeleteAsync(id);

            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Message);
        }

        #endregion

        #region 题目业务逻辑操作

        /// <summary>
        /// 根据标题查找题目
        /// </summary>
        /// <param name="title">题目标题</param>
        /// <returns>题目信息</returns>
        /// <response code="200">成功找到题目</response>
        /// <response code="400">题目未找到或查找失败</response>
        [HttpGet("by-title/{title}")]
        [Authorize(Roles = "Teacher,Admin")]
        public async Task<IActionResult> GetByTitle(string title)
        {
            var result = await _questionService.FindByTitle(title);

            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 批量检查题目标题是否存在
        /// </summary>
        /// <param name="titles">题目标题集合</param>
        /// <returns>标题存在性检查结果</returns>
        /// <response code="200">检查成功</response>
        /// <response code="400">检查失败</response>
        [HttpPost("check-titles")]
        [Authorize(Roles = "Teacher,Admin")]
        public async Task<IActionResult> CheckTitlesExistence([FromBody] List<string> titles)
        {
            var result = await _questionService.CheckTitlesExistence(titles);

            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        #endregion
    }
}
