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>
    /// 知识点管理控制器
    /// 处理知识点相关的操作，如创建、更新、删除知识点等
    /// </summary>
    [Route("api/keypoint")]
    [ApiController]
    public class KeyPointController : ControllerBase
    {
        private readonly IKeyPointService _keyPointService;
        private readonly UserManager<User> _userManager;

        /// <summary>
        /// 初始化知识点控制器
        /// </summary>
        /// <param name="keyPointService">知识点服务</param>
        /// <param name="userManager">用户管理服务</param>
        public KeyPointController(IKeyPointService keyPointService, UserManager<User> userManager)
        {
            _keyPointService = keyPointService;
            _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 _keyPointService.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 _keyPointService.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] KeyPointDto model)
        {
            var result = await _keyPointService.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] KeyPointDto model)
        {
            var result = await _keyPointService.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 = "Teacher,Admin")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var result = await _keyPointService.DeleteAsync(id);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Message);
        }

        #endregion

        #region 知识点扩展操作

        /// <summary>
        /// 根据课程ID获取知识点列表
        /// </summary>
        /// <param name="lessonId">课程ID</param>
        /// <returns>知识点列表</returns>
        /// <response code="200">成功获取知识点列表</response>
        /// <response code="400">获取失败</response>
        [HttpGet("lesson/{lessonId}")]
        [Authorize(Roles = "Student,Teacher,Admin")]
        public async Task<IActionResult> GetByLessonId(Guid lessonId)
        {
            var result = await _keyPointService.GetKeyPointsByLessonIdAsync(lessonId);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        #endregion
    }
}
