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 PracticeSessionController(
    IPracticeSessionService service,
    LearningActorResolver actorResolver) : ControllerBase
{
    [HttpPost("practice-sessions")]
    [EndpointSummary("创建练习会话")]
    [ProducesResponseType<PracticeSessionItem>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
    public async Task<ActionResult<PracticeSessionItem>> CreatePracticeSession(
        CreatePracticeSessionDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.CreatePracticeSessionAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("practice-sessions/detail")]
    [EndpointSummary("获取练习会话详情")]
    [ProducesResponseType<PracticeSessionDetailItem>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<PracticeSessionDetailItem>> GetPracticeSessionDetail(
        [FromQuery] PracticeSessionQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetPracticeSessionDetailAsync(
            actorResolver.Resolve(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpPost("practice-sessions/submit")]
    [EndpointSummary("提交练习会话并生成报告")]
    [ProducesResponseType<PracticeSessionReportItem>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<PracticeSessionReportItem>> SubmitPracticeSession(
        SubmitPracticeSessionDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.SubmitPracticeSessionAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }
}
