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


namespace TechHelper.Server.Controllers
{
	[Route("api/exam")]
	[ApiController]
	[Authorize]
	public class ExamController : ControllerBase
	{
		private readonly IExamService _examService;
		private readonly UserManager<User> _userManager;

		public ExamController(IExamService examService, UserManager<User> userManager)
		{
			_examService = examService;
			_userManager = userManager;
		}

		/// <summary>
		/// 创建一个新的考试/作业。
		/// </summary>
		/// <param name="examDto">考试/作业的数据传输对象。</param>
		/// <returns>新创建的考试/作业信息或错误信息。</returns>
		[HttpPost()]
		[Authorize(Roles = "Teacher")]
		public async Task<IActionResult> AddExam([FromBody] ExamDto examDto)
		{
			var user = await _userManager.FindByEmailAsync(User.Identity.Name);
			if (user == null) return NotFound("没有找到用户");
			examDto.CreatorId = user.Id;
			var result = await _examService.AddAsync(examDto);

			if (result.Status)
			{
				return StatusCode(201, result.Result);
			}
			else
			{
				return BadRequest(result.Message);
			}
		}

		[HttpGet]
		public async Task<IActionResult> GetAllExams([FromQuery] QueryParameter queryParameter)
		{
			var result = await _examService.GetAllAsync(queryParameter);

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

		/// <summary>
		/// 根据ID获取考试/作业详情。
		/// </summary>
		/// <param name="id">考试/作业ID。</param>
		/// <returns>考试/作业详情或未找到错误。</returns>
		[HttpGet("{id:guid}")]
		public async Task<IActionResult> GetExamById(Guid id)
		{
			var result = await _examService.GetAsync(id);

			if (result.Status)
			{
				if (result.Result == null)
				{
					return NotFound("未找到指定的考试/作业。");
				}
				return Ok(result.Result);
			}
			else
			{
				if (result.Message.Contains("未找到") || result.Message.Contains("not found", StringComparison.OrdinalIgnoreCase))
				{
					return NotFound(result.Message);
				}
				return BadRequest(result.Message);
			}
		}

		[HttpDelete("{id:guid}")]
		[Authorize(Roles = "Teacher")]
		public async Task<IActionResult> DeleteAsync(Guid id)
		{
			var deleteResult = await _examService.DeleteAsync(id);

			if (deleteResult.Status)
			{
				return NoContent();
			}
			else
			{
				if (deleteResult.Message.Contains("未找到") || deleteResult.Message.Contains("not found", StringComparison.OrdinalIgnoreCase))
				{
					return NotFound(deleteResult.Message);
				}
				return BadRequest(deleteResult.Message);
			}
		}

		[HttpPost("GetTotalDistributionInClass")]
		[Authorize(Roles = "Teacher")]
		public async Task<IActionResult> GetTotalDistributionInClass(AssigExamToClassDto dto)
		{
			var result = await _examService.GetExamTotalErrorDistributionInClassAsync(dto);

			if (result.Status)
			{
				return Ok(result.Result);
			}
			else
			{
				if (result.Message.Contains("未找到") || result.Message.Contains("not found", StringComparison.OrdinalIgnoreCase))
				{
					return NotFound(result.Message);
				}
				return BadRequest(result.Message);
			}
		}

		/// <summary>
		/// 为指定学生指派作业
		/// </summary>
		/// <param name="AETSdto"></param>
		/// <returns></returns>
		[HttpPost("assignmentExamToStudents")]
		[Authorize(Roles = "Teacher")]
		public async Task<IActionResult> AssignmentExamToStudent([FromBody] AssigExamToStudentsDto AETSdto)
		{
			var result = await _examService.AssignmentToStudentsAsync(AETSdto);
			if (result.Status)
			{
				return Ok(result.Result);
			}
			else
			{
				return BadRequest(result.Message);
			}
		}


		/// <summary>
		/// 为所有学生指派作业
		/// </summary>
		/// <param name="AETSdto"></param>
		/// <returns></returns>
		[HttpPost("assignmentExamToClass")]
		[Authorize(Roles = "Teacher")]
		public async Task<IActionResult> AssignmentExamToClassAsync([FromBody] AssigExamToClassDto AETSdto)
		{
			var user = await _userManager.FindByEmailAsync(User.Identity.Name ?? "");
			var result = await _examService.AssignmentToClassAsync(user.Id, AETSdto.examId, AETSdto.classId);
			if (result.Status)
			{
				return Ok(result.Result);
			}
			else
			{
				return BadRequest(result.Message);
			}
		}



		[HttpGet("GetExamSubmissionDetailInClassAsync")]
		public async Task<IActionResult> GetExamSubmissionDetailInClassAsync([FromBody] AssigExamToClassDto model)
		{
			var result = await _examService.GetExamSubmissionDetailInClassAsync(model);
			if (result.Status)
			{
				return Ok(result.Result);
			}
			else
			{
				return BadRequest(result.Message);
			}
		}
	}
}