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
{
    [Route("api/submission-detail")]
    [ApiController]
    [Authorize]
    public class SubmissionDetailController : ControllerBase
    {
        private readonly UserManager<User> _userManager;
        private readonly ISubmissionDetailService _submissionDetailService;

        public SubmissionDetailController(UserManager<User> userManager, ISubmissionDetailService submissionDetailService)
        {
            _userManager = userManager;
            _submissionDetailService = submissionDetailService;
        }

        #region 基本CRUD操作

        /// <summary>
        /// 获取所有提交详情
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <returns>提交详情列表</returns>
        [HttpGet()]
        [Authorize(Roles = "Admin,Teacher")]
        public async Task<IActionResult> GetAllAsync([FromQuery] QueryParameter query)
        {
            var result = await _submissionDetailService.GetAllAsync(query);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 根据ID获取提交详情
        /// </summary>
        /// <param name="id">提交详情ID</param>
        /// <returns>提交详情详情</returns>
        [HttpGet("{id:guid}")]
        public async Task<IActionResult> GetAsync(Guid id)
        {
            var result = await _submissionDetailService.GetAsync(id);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 创建提交详情
        /// </summary>
        /// <param name="model">提交详情数据传输对象</param>
        /// <returns>创建结果</returns>
        [HttpPost()]
        public async Task<IActionResult> AddAsync([FromBody] SubmissionDetailDto model)
        {
            var result = await _submissionDetailService.AddAsync(model);
            return result.Status ? StatusCode(201, result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 更新提交详情
        /// </summary>
        /// <param name="id">提交详情ID</param>
        /// <param name="model">提交详情数据传输对象</param>
        /// <returns>更新结果</returns>
        [HttpPut("{id:guid}")]
        public async Task<IActionResult> UpdateAsync(Guid id, [FromBody] SubmissionDetailDto model)
        {
            if (id != model.Id)
            {
                return BadRequest("路由ID与请求体中的ID不匹配。");
            }

            var result = await _submissionDetailService.UpdateAsync(model);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 删除提交详情
        /// </summary>
        /// <param name="id">提交详情ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("delete/{id:guid}")]
        public async Task<IActionResult> DeleteAsync(Guid id)
        {
            var result = await _submissionDetailService.DeleteAsync(id);
            return result.Status ? NoContent() : BadRequest(result.Message);
        }

        #endregion

        #region 特殊操作

        /// <summary>
        /// 根据提交ID获取提交详情列表
        /// </summary>
        /// <param name="submissionId">提交ID</param>
        /// <returns>提交详情列表</returns>
        [HttpGet("by-submission/{submissionId:guid}")]
        public async Task<IActionResult> GetBySubmissionIdAsync(Guid submissionId)
        {
            var result = await _submissionDetailService.GetBySubmissionIdAsync(submissionId);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 根据学生ID和考试ID获取提交详情
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="examId">考试ID</param>
        /// <returns>提交详情列表</returns>
        [HttpGet("by-student-exam")]
        public async Task<IActionResult> GetByStudentAndExamAsync([FromQuery] Guid studentId, [FromQuery] Guid examId)
        {
            var result = await _submissionDetailService.GetByStudentAndExamAsync(studentId, examId);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 批量创建提交详情
        /// </summary>
        /// <param name="submissionId">提交ID</param>
        /// <param name="details">提交详情列表</param>
        /// <returns>创建结果</returns>
        [HttpPost("batch-create/{submissionId:guid}")]
        public async Task<IActionResult> BatchCreateAsync(Guid submissionId, [FromBody] List<SubmissionDetailDto> details)
        {
            var result = await _submissionDetailService.BatchCreateAsync(submissionId, details);
            return result.Status ? StatusCode(201, result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 批量更新提交详情
        /// </summary>
        /// <param name="details">提交详情列表</param>
        /// <returns>更新结果</returns>
        [HttpPut("batch-update")]
        public async Task<IActionResult> BatchUpdateAsync([FromBody] List<SubmissionDetailDto> details)
        {
            var result = await _submissionDetailService.BatchUpdateAsync(details);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 更新提交详情评分
        /// </summary>
        /// <param name="detailId">提交详情ID</param>
        /// <param name="points">分数</param>
        /// <param name="feedback">反馈</param>
        /// <returns>更新结果</returns>
        [HttpPut("{detailId:guid}/score")]
        public async Task<IActionResult> UpdateScoreAsync(Guid detailId, [FromQuery] float? points, [FromQuery] string? feedback)
        {
            var result = await _submissionDetailService.UpdateScoreAsync(detailId, points, feedback);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 获取错题详情
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="examId">考试ID</param>
        /// <returns>错题详情列表</returns>
        [HttpGet("error-details")]
        [Authorize(Roles = "Student,Teacher")]
        public async Task<IActionResult> GetErrorDetailsAsync([FromQuery] Guid studentId, [FromQuery] Guid examId)
        {
            var result = await _submissionDetailService.GetErrorDetailsAsync(studentId, examId);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 获取正确题详情
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="examId">考试ID</param>
        /// <returns>正确题详情列表</returns>
        [HttpGet("correct-details")]
        [Authorize(Roles = "Student,Teacher")]
        public async Task<IActionResult> GetCorrectDetailsAsync([FromQuery] Guid studentId, [FromQuery] Guid examId)
        {
            var result = await _submissionDetailService.GetCorrectDetailsAsync(studentId, examId);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 获取未批改的提交详情
        /// </summary>
        /// <returns>未批改的提交详情列表</returns>
        [HttpGet("ungraded")]
        [Authorize(Roles = "Teacher")]
        public async Task<IActionResult> GetUngradedDetailsAsync()
        {
            var user = await _userManager.FindByEmailAsync(User.Identity.Name);
            if (user == null)
            {
                return NotFound("未找到当前用户信息。");
            }

            var result = await _submissionDetailService.GetUngradedDetailsAsync(user.Id);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        /// <summary>
        /// 批量更新提交详情状态
        /// </summary>
        /// <param name="submissionId">提交ID</param>
        /// <param name="status">状态</param>
        /// <returns>更新结果</returns>
        [HttpPut("{submissionId:guid}/status")]
        public async Task<IActionResult> UpdateStatusAsync(Guid submissionId, [FromQuery] SubmissionStatus status)
        {
            var result = await _submissionDetailService.UpdateStatusAsync(submissionId, status);
            return result.Status ? Ok(result) : BadRequest(result.Message);
        }

        #endregion
    }
}
