using YBEE.EQM.Core;

namespace YBEE.EQM.Application;

/// <summary>
/// 学生成绩导入服务
/// </summary>
[ApiDescriptionSettings(Name = "exam-score-import")]
[Route("exam/score/import")]
public class ExamScoreImportAppService(IExamScoreImportService service) : IDynamicApiController
{
    private readonly IExamScoreImportService _examScoreImportService = service;

    /// <summary>
    /// 上传文件并完成批量导入前期未上报学生名单的各科成绩
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task UploadImportWithoutStudentTotalScore([FromForm] UploadExamDataInput input)
    {
        string fileExt = Path.GetExtension(input.File.FileName).ToLower();
        if (fileExt != ".xls" && fileExt != ".xlsx")
        {
            throw Oops.Oh(ErrorCode.E1010);
        }

        string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
        using FileStream fs = File.Create(filePath);
        await input.File.CopyToAsync(fs);
        await fs.FlushAsync();
        fs.Close();
        await _examScoreImportService.UploadImportWithoutStudentTotalScore(filePath, input.ExamPlanId);
    }

    /// <summary>
    /// 批量导入学生总成绩
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task UploadImportStudentTotalScore([FromForm] UploadExamDataInput input)
    {
        string fileExt = Path.GetExtension(input.File.FileName).ToLower();
        if (fileExt != ".xls" && fileExt != ".xlsx")
        {
            throw Oops.Oh(ErrorCode.E1010);
        }

        string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
        using FileStream fs = File.Create(filePath);
        await input.File.CopyToAsync(fs);
        await fs.FlushAsync();
        fs.Close();
        await _examScoreImportService.UploadImportStudentTotalScore(filePath, input.ExamPlanId);
    }

    /// <summary>
    /// 导入区校合并小题成绩
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task<FileContentResult> UploadImportStudentMinorScore([FromForm] UploadExamDataInput input)
    {
        string fileExt = Path.GetExtension(input.File.FileName).ToLower();
        if (fileExt != ".zip")
        {
            throw Oops.Oh(ErrorCode.E1010);
        }

        string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
        using FileStream fs = File.Create(filePath);
        await input.File.CopyToAsync(fs);
        await fs.FlushAsync();
        fs.Close();

        var (fileName, fileBytes) = await _examScoreImportService.UploadImportStudentMinorScore(filePath, input.ExamPlanId);
        return new FileContentResult(fileBytes, "application/octet-stream")
        {
            FileDownloadName = fileName,
        };
    }
}
