using Exam.Infrastructure.Data.Entity;
using Exam.Share;
using Exam.Share.ViewResponses.Exam;
using Hotline.Application.Exam.Constants.ApiRoutes;
using Hotline.Application.Exam.Interface.ExamManages;
using Hotline.Repository.SqlSugar.Exam.Interface;
using Hotline.Share.Requests.Exam;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hotline.Api.Controllers.Exam
{
    public class ExamManageController : BaseController
    {
        private readonly IExamManageService _examManageService;
        public ExamManageController(IExamManageService examManageService)
        {
            _examManageService = examManageService;
        }

        /// <summary>
        /// 新增考试
        /// </summary>
        /// <param name="questionDto"></param>
        /// <returns></returns>
        [HttpPost(ExamManageApiRoute.Add)]
        public async Task Add([FromBody] AddExamManageDto questionDto)
        {
            await _examManageService.AddAsync(questionDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 修改考试
        /// </summary>
        /// <param name="questionDto"></param>
        /// <returns></returns>
        [HttpPut(ExamManageApiRoute.Update)]
        public async Task Update([FromBody] UpdateExamManageDto questionDto)
        {
            await _examManageService.UpdateAsync(questionDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 删除考试
        /// </summary>
        /// <param name="entityQueryRequest"></param>
        /// <returns></returns>
        [HttpDelete(ExamManageApiRoute.Delete)]
        public async Task Delete([FromBody] EntityQueryRequest entityQueryRequest)
        {
            await _examManageService.DeleteAsync(entityQueryRequest, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 获取考试分页列表
        /// </summary>
        /// <param name="questionPagedRequest"></param>
        /// <returns></returns>
        [HttpPost(ExamManageApiRoute.GetPagedList)]
        public async Task<ExamManagePageViewResponse> GetPagedList([FromBody] ExamManagePagedRequest questionPagedRequest)
        {
            await _examManageService.UpdateExamStatus(new EntityQueryRequest(), HttpContext.RequestAborted);

            var questionPageViewResponse = await _examManageService.GetPagedListAsync(questionPagedRequest);

            return questionPageViewResponse as ExamManagePageViewResponse;
        }

        /// <summary>
        /// 获取考试
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet(ExamManageApiRoute.Get)]
        public async Task<ExamManageDto> Get(string id)
        {
            var questionDto = await _examManageService.GetAsync(new EntityQueryRequest
            {
                Id = id
            });

            return questionDto;
        }

        /// <summary>
        /// 组卷
        /// </summary>
        /// <param name="generateExamTestPaperRequest"></param>
        /// <returns></returns>
        [HttpPost(ExamManageApiRoute.GenerateTestPaper)]
        public async Task GenerateTestPaper([FromBody]GenerateExamTestPaperRequest generateExamTestPaperRequest)
        {
            await _examManageService.GenerateTestPaper(generateExamTestPaperRequest, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 更新开启状态
        /// </summary>
        /// <param name="statusActionRequests"></param>
        /// <returns></returns>
        [HttpPost(ExamManageApiRoute.UpdateStatus)]
        public async Task UpdateStatus([FromBody] List<StatusActionRequest> statusActionRequests)
        {
            await _examManageService.UpdateStatus(statusActionRequests, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 更新考试状态
        /// </summary>
        /// <param name="entityQueryRequest"></param>
        /// <returns></returns>
        [HttpPost(ExamManageApiRoute.UpdateExamStatus)]
        public async Task UpdateExamStatus([FromBody] EntityQueryRequest entityQueryRequest)
        {
            await _examManageService.UpdateExamStatus(entityQueryRequest, HttpContext.RequestAborted);
        }
    }
}
