using Entities.Contracts;
using Entities.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using TechHelper.Services.Beta;

namespace TechHelper.Server.Controllers
{
    [Route("api/student-submission")]
    [ApiController]
    [Authorize]
    public class StudentSubmissionController : ControllerBase
    {
        private readonly ISubmissionService _studentSubmissionService;
        private readonly UserManager<User> _userManager;

        public StudentSubmissionController(
			ISubmissionService studentSubmissionService,
            UserManager<User> userManager)
        {
            _studentSubmissionService = studentSubmissionService;
            _userManager = userManager;
        }

        /// <summary>
        /// 获取当前学生的所有提交摘要
        /// </summary>
        /// <returns>学生提交摘要列表</returns>
        [HttpGet("my-submissions")]
        public async Task<IActionResult> GetMySubmissions()
        {
            var user = await _userManager.FindByEmailAsync(User.Identity.Name);
            if (user == null)
                return NotFound("未找到用户信息");

            var result = await _studentSubmissionService.GetStudentSubmissionDetailAsync(user.Id);

            if (result.Status)
            {
                return Ok(result.Result);
            }
            else
            {
                return BadRequest(result.Message);
            }
        }

        /// <summary>
        /// 获取当前学生的提交摘要（分页）
        /// </summary>
        /// <param name="pageNumber">页码，默认为1</param>
        /// <param name="pageSize">每页数量，默认为10</param>
        /// <returns>分页的学生提交摘要列表</returns>
        [HttpGet("my-submissions-paged")]
        public async Task<IActionResult> GetMySubmissionsPaged(int pageNumber = 1, int pageSize = 10)
        {
            if (pageNumber < 1) pageNumber = 1;
            if (pageSize < 1) pageSize = 10;
            if (pageSize > 100) pageSize = 100; // 限制最大页面大小

            var user = await _userManager.FindByEmailAsync(User.Identity.Name);
            if (user == null)
                return NotFound("未找到用户信息");

            var result = await _studentSubmissionService.GetStudentSubmissionSummariesAsync(user.Id);

            if (result.Status)
            {
                return Ok(result.Result);
            }
            else
            {
                return BadRequest(result.Message);
            }
        }

        /// <summary>
        /// 获取指定学生的提交摘要（仅教师可使用）
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <returns>学生提交摘要列表</returns>
        [HttpGet("student/{studentId:guid}")]
        [Authorize(Roles = "Teacher")]
        public async Task<IActionResult> GetStudentSubmissions(Guid studentId)
        {
            var result = await _studentSubmissionService.GetStudentSubmissionSummariesAsync(studentId);

            if (result.Status)
            {
                return Ok(result.Result);
            }
            else
            {
                return BadRequest(result.Message);
            }
        }

        /// <summary>
        /// 获取指定学生的提交摘要（分页，仅教师可使用）
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="pageNumber">页码，默认为1</param>
        /// <param name="pageSize">每页数量，默认为10</param>
        /// <returns>分页的学生提交摘要列表</returns>
        [HttpGet("student/{studentId:guid}/paged")]
        [Authorize(Roles = "Teacher")]
        public async Task<IActionResult> GetStudentSubmissionsPaged(Guid studentId, int pageNumber = 1, int pageSize = 10)
        {
            if (pageNumber < 1) pageNumber = 1;
            if (pageSize < 1) pageSize = 10;
            if (pageSize > 100) pageSize = 100; // 限制最大页面大小

            var result = await _studentSubmissionService.GetStudentSubmissionSummariesAsync(studentId);

            if (result.Status)
            {
                return Ok(result.Result);
            }
            else
            {
                return BadRequest(result.Message);
            }
        }
    }
}
