using Entities.Contracts;
using Entities.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Security.Claims;
using TechHelper.Services;

namespace TechHelper.Server.Controllers
{
	/// <summary>
	/// 班级管理控制器
	/// 处理班级相关的操作，如用户注册到班级、获取班级学生等
	/// </summary>
	[Route("api/class")]
	[ApiController]
	public class ClassController : ControllerBase
	{
		private IClassService _classService;
		private UserManager<User> _userManager;

		/// <summary>
		/// 初始化班级控制器
		/// </summary>
		/// <param name="classService">班级服务</param>
		/// <param name="userManager">用户管理服务</param>
		public ClassController(IClassService classService, UserManager<User> userManager)
		{
			_classService = classService;
			_userManager = userManager;
		}

		/// <summary>
		/// 用户注册到班级
		/// </summary>
		/// <param name="toClass">用户注册到班级的数据传输对象</param>
		/// <returns>操作结果</returns>
		/// <response code="200">注册成功</response>
		/// <response code="400">注册失败</response>
		[HttpPost("userRegiste")]
		public async Task<IActionResult> UserRegisterToClass(
			[FromBody] UserRegistrationToClassDto toClass)
		{

			var result = await _classService.UserRegister(toClass);

			if (!result.Status) return BadRequest(result.Message);

			return Ok();
		}




		/// <summary>
		/// 获取班级学生列表
		/// 仅限教师角色访问，根据教师所在班级信息获取学生列表
		/// </summary>
		/// <returns>班级学生列表</returns>
		/// <response code="200">成功获取学生列表</response>
		/// <response code="400">权限不足或班级信息缺失</response>
		[HttpPost("getClassStudents")]
		public async Task<IActionResult> GetClassStudents()
		{
			if (User.IsInRole("Teacher"))
			{
				var gradeClaim = User.FindFirst("Grade")?.Value;
				var classClaim = User.FindFirst("Class")?.Value;

				if (string.IsNullOrEmpty(gradeClaim) || string.IsNullOrEmpty(classClaim))
				{
					return BadRequest("未识别到你加入的班级信息（年级或班级声明缺失）。");
				}

				if (!byte.TryParse(gradeClaim, out byte grade) || !byte.TryParse(classClaim, out byte cla))
				{
					return BadRequest("你班级或年级信息格式不正确。");
				}

				var classDto = new ClassDto
				{
					Grade = grade,
					Class = cla
				};

				var result = await _classService.GetClassStudents(classDto);
				var css = result.Result as ICollection<ClassStudent>;
				if (css == null) return BadRequest("你还没有学生");


				List<StudentDto> sts = new List<StudentDto>();
				css?.ToList().ForEach(s => sts.Add(new StudentDto
				{
					DisplayName = s.Student.DisplayName,
					Id = s.Student.Id,
				}));

				if (!result.Status)
				{
					return BadRequest(result.Message);
				}
				return Ok(sts);
			}
			else
			{
				return BadRequest("你没有权限查看，未识别到你的教师身份。");
			}
		}

		/// <summary>
		/// 创建新班级
		/// </summary>
		/// <param name="classDto">班级数据传输对象</param>
		/// <returns>操作结果</returns>
		/// <response code="200">班级创建成功</response>
		/// <response code="400">班级创建失败</response>
		[HttpPost("Create")]
		public async Task<IActionResult> Create(
			[FromBody] ClassDto classDto)
		{
			var result = await _classService.AddAsync(classDto);
			if (!result.Status) return BadRequest(result.Message);

			return Ok();
		}

		/// <summary>
		/// 获取指定年级的所有班级列表
		/// </summary>
		/// <param name="classDto">年级编号</param>
		/// <returns>班级列表</returns>
		/// <response code="200">成功获取班级列表</response>
		/// <response code="400">获取失败</response>
		[HttpPost("GetGradeClasses")]
		public async Task<IActionResult> GetGradeClasses(
		[FromBody] byte classDto)
		{
			var result = await _classService.GetGradeClasses(classDto);
			if (!result.Status) return BadRequest(result.Message);

			return Ok(result.Result);
		}
	}
}
