using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Api.Security;
using Tiku.Application.Learning;
using Tiku.Application.Security;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("学生端-学习")]
[Authorize(Policy = TikuPolicies.CurrentTenantMember)]
[RequireSaasFeature(SaasFeatureCatalog.Practice)]
[Produces("application/json")]
[Route("api/student/learning")]
public sealed class PracticeReportController(
    IPracticeReportService service,
    LearningActorResolver actorResolver) : ControllerBase
{
    [HttpGet("practice-sessions/report")]
    [EndpointSummary("获取练习会话报告")]
    [ProducesResponseType<PracticeSessionReportItem>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<PracticeSessionReportItem>> GetPracticeSessionReport(
        [FromQuery] PracticeSessionQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetPracticeSessionReportAsync(
            actorResolver.Resolve(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpGet("practice-reports")]
    [EndpointSummary("查询练习报告列表")]
    [ProducesResponseType<LearningList<PracticeSessionReportItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningList<PracticeSessionReportItem>>> GetPracticeReports(
        [FromQuery] PracticeSessionQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetPracticeReportsAsync(
            actorResolver.Resolve(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpGet("practice-sessions/history")]
    [EndpointSummary("查询练习历史")]
    [ProducesResponseType<LearningList<PracticeHistoryItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningList<PracticeHistoryItem>>> GetPracticeHistory(
        [FromQuery] PracticeSessionQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetPracticeHistoryAsync(
            actorResolver.Resolve(),
            query.ToFilter(),
            cancellationToken));
    }
}
