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/Lesson")]
    [ApiController]
    public class LessonController : ControllerBase
    {
        private readonly ILessonService _lessonService;
        private readonly UserManager<User> _userManager;

        /// <summary>
        /// 初始化课程控制器
        /// </summary>
        /// <param name="lessonService">课程服务</param>
        /// <param name="userManager">用户管理服务</param>
        public LessonController(ILessonService lessonService, UserManager<User> userManager)
        {
            _lessonService = lessonService;
            _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 _lessonService.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 _lessonService.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] LessonDto model)
        {
            var result = await _lessonService.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] LessonDto model)
        {
            var result = await _lessonService.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 _lessonService.DeleteAsync(id);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Message);
        }

        #endregion

        #region 课程扩展操作

        /// <summary>
        /// 根据教材ID获取课程列表
        /// </summary>
        /// <param name="textbookId">教材ID</param>
        /// <returns>课程列表</returns>
        /// <response code="200">成功获取课程列表</response>
        /// <response code="400">获取失败</response>
        [HttpGet("textbook/{textbookId}")]
        [Authorize(Roles = "Student,Teacher,Admin")]
        public async Task<IActionResult> GetByTextbookId(Guid textbookId)
        {
            var result = await _lessonService.GetAllAsync(new QueryParameter { Search = textbookId.ToString() });
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        #endregion
    }
}
