using Newtonsoft.Json;
using Entities.DTO;

namespace TechHelper.Client.Services
{
    public class StudentSubmissionService : IStudentSubmissionService
    {
        private readonly HttpClient _client;

        public StudentSubmissionService(HttpClient client)
        {
            _client = client;
        }

        /// <summary>
        /// 获取当前学生的所有提交摘要
        /// </summary>
        /// <returns>学生提交摘要列表</returns>
        public async Task<ApiResponse> GetMySubmissionsAsync()
        {
            try
            {
                var response = await _client.GetAsync("student-submission/my-submissions");
                
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    var submissions = JsonConvert.DeserializeObject<List<StudentSubmissionSummaryDto>>(content);
                    return ApiResponse.Success(result: submissions);
                }
                else
                {
                    var errorContent = await response.Content.ReadAsStringAsync();
                    return ApiResponse.Error(message: $"获取学生提交信息失败: {response.StatusCode} - {errorContent}");
                }
            }
            catch (Exception ex)
            {
                return ApiResponse.Error(message: $"内部错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取当前学生的提交摘要（分页）
        /// </summary>
        /// <param name="pageNumber">页码，默认为1</param>
        /// <param name="pageSize">每页数量，默认为10</param>
        /// <returns>分页的学生提交摘要列表</returns>
        public async Task<ApiResponse> GetMySubmissionsPagedAsync(int pageNumber = 1, int pageSize = 10)
        {
            try
            {
                var response = await _client.GetAsync($"student-submission/my-submissions-paged?pageNumber={pageNumber}&pageSize={pageSize}");
                
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    var result = JsonConvert.DeserializeObject<StudentSubmissionSummaryResponseDto>(content);
                    return ApiResponse.Success(result: result);
                }
                else
                {
                    var errorContent = await response.Content.ReadAsStringAsync();
                    return ApiResponse.Error(message: $"获取学生提交信息失败: {response.StatusCode} - {errorContent}");
                }
            }
            catch (Exception ex)
            {
                return ApiResponse.Error(message: $"内部错误: {ex.Message}");
            }
        }
    }
}
