using Exam.Application.Interface.Exam;
using Exam.Infrastructure.Data.Entity;
using Exam.Share.ViewResponses.Exam;
using Exam.Share.ViewResponses.TestPaper;
using Hotline.Application.Exam.Constants.ApiRoutes;
using Hotline.Application.Exam.Interface.TestPapers;
using Hotline.Application.Exam.Service.ExamManages;
using Hotline.Repository.SqlSugar.Exam.Interface;
using Hotline.Share.Dtos.Questions;
using Hotline.Share.Dtos.TestPapers;
using Hotline.Share.Requests.Exam;
using Hotline.Share.Requests.TestPaper;
using Hotline.Share.ViewResponses.Exam;
using Hotline.Share.ViewResponses.Questions;
using Mapster;
using Microsoft.AspNetCore.Mvc;

namespace Hotline.Api.Controllers.Exam
{
    public class TestPaperController : BaseController
    {
        private readonly ITestPaperService _testPaperService;
        public TestPaperController(ITestPaperService testPaperService)
        {
            _testPaperService = testPaperService;
        }

        /// <summary>
        /// 新增试卷
        /// </summary>
        /// <param name="testPaperDto"></param>
        /// <returns></returns>
        [HttpPost(TestPaperApiRoute.Add)]
        public async Task Add([FromBody] AddTestPaperDto testPaperDto)
        {
            await _testPaperService.AddAsync(testPaperDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 修改试卷
        /// </summary>
        /// <param name="testPaperDto"></param>
        /// <returns></returns>
        [HttpPut(TestPaperApiRoute.Update)]
        public async Task Update([FromBody] UpdateTestPaperDto testPaperDto)
        {
            await _testPaperService.UpdateAsync(testPaperDto, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 删除试卷
        /// </summary>
        /// <param name="entityQueryRequest"></param>
        /// <returns></returns>
        [HttpDelete(TestPaperApiRoute.Delete)]

        public async Task Delete(EntityQueryRequest entityQueryRequest)
        {
            await _testPaperService.DeleteAsync(entityQueryRequest, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 获取试卷分页列表
        /// </summary>
        /// <param name="sourcewarePagedRequest"></param>
        /// <returns></returns>
        [HttpPost(TestPaperApiRoute.GetPagedList)]
        public async Task<TestPaperPageViewResponse> GetPagedList([FromBody] TestPaperPagedRequest sourcewarePagedRequest)
        {
            var testPaperPageViewResponse = await _testPaperService.GetPagedListAsync(sourcewarePagedRequest);

            return testPaperPageViewResponse as TestPaperPageViewResponse;
        }

        /// <summary>
        /// 获取试卷列表
        /// </summary>
        /// <param name="testPaperRequest"></param>
        /// <returns></returns>
        [HttpGet(TestPaperApiRoute.GetList)]
        public async Task<List<TestPaperViewResponse>> GetList([FromQuery] TestPaperRequest testPaperRequest)
        {
            var testPaperPagedRequest = testPaperRequest.Adapt<TestPaperPagedRequest>();

            return (await _testPaperService.GetListAsync(testPaperPagedRequest)).Item2;
        }


        /// <summary>
        /// 获取试卷分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet(TestPaperApiRoute.Get)]
        public async Task<TestPaperDto> Get(string id)
        {
            var testPaperDto = await _testPaperService.GetAsync(new EntityQueryRequest
            {
                Id = id
            });

            return testPaperDto;
        }

        /// <summary>
        /// 获取试卷试题数
        /// </summary>
        /// <returns></returns>
        [HttpGet(TestPaperApiRoute.Count)]
        public async Task<List<TestPaperQuestionCountViewResponse>> GetPagedList([FromQuery] TestPaperQuestionCountRequest testPaperQuestionCountRequest)
        {
            var testPaperQuestionCountViewResponses = await _testPaperService.GetTestPaperQuestionCount(testPaperQuestionCountRequest);

            return testPaperQuestionCountViewResponses;
        }

        /// <summary>
        /// 根据规则获取试题
        /// </summary>
        /// <param name="testPaperQuestionRequest"></param>
        /// <returns></returns>
        [HttpPost(TestPaperApiRoute.GetQuestions)]
        public async Task<List<SimpleQuestionViewResponse>> GetQuestions([FromBody]TestPaperQuestionRequest testPaperQuestionRequest)
        {
           return await _testPaperService.GetQuestionDtos(testPaperQuestionRequest);
        }

        /// <summary>
        /// 获取标签试题数
        /// </summary>
        /// <param name="tagQuestionCountRequest"></param>
        /// <returns></returns>
        [HttpPost(TestPaperApiRoute.GetTagQuestionCount)]
        public async Task<TagQuestionCountViewResponse> GetTagQuestionCount([FromBody] TagQuestionCountRequest tagQuestionCountRequest)
        {
            return await _testPaperService.GetTagQuestionCount(tagQuestionCountRequest);
        }

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