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 LearningAnalyticsController(
    ILearningAnalyticsService service,
    LearningActorResolver actorResolver) : ControllerBase
{
    [HttpGet("stats")]
    [EndpointSummary("查询学习统计")]
    [ProducesResponseType<LearningStatsItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningStatsItem>> GetStats(CancellationToken cancellationToken)
    {
        return Ok(await service.GetStatsAsync(actorResolver.Resolve(), cancellationToken));
    }

    [HttpGet("trend")]
    [EndpointSummary("查询学习趋势")]
    [ProducesResponseType<LearningList<LearningTrendItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningList<LearningTrendItem>>> GetTrend(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetTrendAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpGet("leaderboard")]
    [EndpointSummary("查询学习排行榜")]
    [ProducesResponseType<LearningLeaderboardResult>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningLeaderboardResult>> GetLeaderboard(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(
            await service.GetLeaderboardAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }
}
