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/textbook")]
    [ApiController]
    public class TextbookController : ControllerBase
    {
        private readonly ITextbookService _textbookService;
        private readonly UserManager<User> _userManager;

        /// <summary>
        /// 初始化教材控制器
        /// </summary>
        /// <param name="textbookService">教材服务</param>
        /// <param name="userManager">用户管理服务</param>
        public TextbookController(ITextbookService textbookService, UserManager<User> userManager)
        {
            _textbookService = textbookService;
            _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 _textbookService.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 _textbookService.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] TextbookDto model)
        {
            var result = await _textbookService.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] TextbookDto model)
        {
            var result = await _textbookService.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 _textbookService.DeleteAsync(id);
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Message);
        }

        #endregion

        #region 教材扩展操作

        /// <summary>
        /// 根据年级获取教材列表
        /// </summary>
        /// <param name="grade">年级</param>
        /// <returns>教材列表</returns>
        /// <response code="200">成功获取教材列表</response>
        /// <response code="400">获取失败</response>
        [HttpGet("grade/{grade}")]
        [Authorize(Roles = "Student,Teacher,Admin")]
        public async Task<IActionResult> GetByGrade(string grade)
        {
            var result = await _textbookService.GetAllAsync(new QueryParameter { Search = grade });
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 根据出版社获取教材列表
        /// </summary>
        /// <param name="publisher">出版社</param>
        /// <returns>教材列表</returns>
        /// <response code="200">成功获取教材列表</response>
        /// <response code="400">获取失败</response>
        [HttpGet("publisher/{publisher}")]
        [Authorize(Roles = "Student,Teacher,Admin")]
        public async Task<IActionResult> GetByPublisher(string publisher)
        {
            var result = await _textbookService.GetAllAsync(new QueryParameter { Search = publisher });
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        /// <summary>
        /// 根据学科领域获取教材列表
        /// </summary>
        /// <param name="subjectArea">学科领域</param>
        /// <returns>教材列表</returns>
        /// <response code="200">成功获取教材列表</response>
        /// <response code="400">获取失败</response>
        [HttpGet("subjectArea/{subjectArea}")]
        [Authorize(Roles = "Student,Teacher,Admin")]
        public async Task<IActionResult> GetBySubjectArea(string subjectArea)
        {
            var result = await _textbookService.GetAllAsync(new QueryParameter { Search = subjectArea });
            
            if (!result.Status)
            {
                return BadRequest(result.Message);
            }

            return Ok(result.Result);
        }

        #endregion
    }
}
