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;
using TechHelper.Services.Beta;
using static TechHelper.Services.Beta.IExamQuestionService;

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

        /// <summary>
        /// 初始化考试题目控制器
        /// </summary>
        /// <param name="examQuestionService">考试题目服务</param>
        /// <param name="userManager">用户管理服务</param>
        public ExamQuestionController(IExamQuestionService examQuestionService, UserManager<User> userManager)
        {
            _examQuestionService = examQuestionService;
            _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 _examQuestionService.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 _examQuestionService.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] ExamQuestionDto model)
        {
            var result = await _examQuestionService.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, Teacher")]
        public async Task<IActionResult> Update([FromBody] ExamQuestionDto model)
        {
            var result = await _examQuestionService.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 _examQuestionService.DeleteAsync(id);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Message);
        }

		#endregion


		[HttpPost("GetQuestionDependy")]
		[Authorize(Roles = "Teacher,Admin")]
		public async Task<IActionResult> GetQuestionDependy(QuesitionDenpendceyRequst request)
		{
			var result = await _examQuestionService.GetQuestionDependy(request);

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

			return Ok(result.Result);
		}
	}
}
