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 LearningController(
    ILearningActivityService learningActivityService,
    ICurrentUser currentUser,
    ITenantContext currentTenant) : ControllerBase
{
    [HttpGet("stats")]
    [EndpointSummary("查询学习统计")]
    [ProducesResponseType<LearningStatsItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningStatsItem>> GetStats(CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.GetStatsAsync(ResolveActor(), cancellationToken));
    }

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

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

    [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 learningActivityService.CreatePracticeSessionAsync(
            ResolveActor(),
            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 learningActivityService.GetPracticeSessionDetailAsync(
            ResolveActor(),
            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 learningActivityService.SubmitPracticeSessionAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    [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 learningActivityService.GetPracticeSessionReportAsync(
            ResolveActor(),
            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 learningActivityService.GetPracticeReportsAsync(
            ResolveActor(),
            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 learningActivityService.GetPracticeHistoryAsync(
            ResolveActor(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpPost("answers")]
    [EndpointSummary("提交题目答案")]
    [ProducesResponseType<AnswerRecordItem>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status403Forbidden)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<AnswerRecordItem>> SubmitAnswer(
        SubmitAnswerDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.SubmitAnswerAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("favorites/questions")]
    [EndpointSummary("查询收藏题目")]
    [ProducesResponseType<LearningList<FavoriteQuestionItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningList<FavoriteQuestionItem>>> GetFavoriteQuestions(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.GetFavoriteQuestionsAsync(
            ResolveActor(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpPost("favorites/questions")]
    [EndpointSummary("收藏或取消收藏题目")]
    [ProducesResponseType<LearningActionResult>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<LearningActionResult>> ToggleFavoriteQuestion(
        QuestionActionDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.ToggleFavoriteQuestionAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("wrong-questions")]
    [EndpointSummary("查询错题列表")]
    [ProducesResponseType<LearningList<WrongQuestionItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningList<WrongQuestionItem>>> GetWrongQuestions(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.GetWrongQuestionsAsync(
            ResolveActor(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpGet("wrong-questions/review-plan")]
    [EndpointSummary("生成错题复习计划")]
    [ProducesResponseType<WrongQuestionReviewPlan>(StatusCodes.Status200OK)]
    public async Task<ActionResult<WrongQuestionReviewPlan>> GetWrongQuestionReviewPlan(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.GetWrongQuestionReviewPlanAsync(
            ResolveActor(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpPost("wrong-questions/resolve")]
    [EndpointSummary("将错题标记为已解决")]
    [ProducesResponseType<LearningActionResult>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<LearningActionResult>> ResolveWrongQuestion(
        QuestionActionDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.ResolveWrongQuestionAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("vocabulary/progress")]
    [EndpointSummary("查询单词学习进度")]
    [ProducesResponseType<LearningList<WordProgressItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningList<WordProgressItem>>> GetWordProgress(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.GetWordProgressAsync(
            ResolveActor(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpGet("vocabulary/review-plan")]
    [EndpointSummary("生成单词复习计划")]
    [ProducesResponseType<WordReviewPlan>(StatusCodes.Status200OK)]
    public async Task<ActionResult<WordReviewPlan>> GetWordReviewPlan(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.GetWordReviewPlanAsync(
            ResolveActor(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpPost("vocabulary/progress")]
    [EndpointSummary("更新单词学习进度")]
    [ProducesResponseType<WordProgressItem>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<WordProgressItem>> UpdateWordProgress(
        WordProgressDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.UpdateWordProgressAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("vocabulary/review")]
    [EndpointSummary("提交单词复习结果")]
    [ProducesResponseType<WordProgressItem>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<WordProgressItem>> ReviewWord(
        WordReviewDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.ReviewWordAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("vocabulary/stats")]
    [EndpointSummary("查询单词学习统计")]
    [ProducesResponseType<WordStatsItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<WordStatsItem>> GetWordStats(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.GetWordStatsAsync(
            ResolveActor(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpGet("vocabulary/favorites")]
    [EndpointSummary("查询收藏单词")]
    [ProducesResponseType<LearningList<FavoriteWordItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<LearningList<FavoriteWordItem>>> GetFavoriteWords(
        [FromQuery] LearningLimitQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.GetFavoriteWordsAsync(
            ResolveActor(),
            query.ToFilter(),
            cancellationToken));
    }

    [HttpPost("vocabulary/favorites")]
    [EndpointSummary("收藏或取消收藏单词")]
    [ProducesResponseType<LearningActionResult>(StatusCodes.Status200OK)]
    [ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<LearningActionResult>> ToggleFavoriteWord(
        FavoriteWordDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await learningActivityService.ToggleFavoriteWordAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    private LearningActor ResolveActor()
    {
        if (currentTenant.TenantId is null || currentUser.UserId is null) throw new LearningAccessDeniedException();

        return new LearningActor(currentTenant.TenantId.Value, currentUser.UserId.Value);
    }
}

public sealed class LearningAccessDeniedException()
    : Exception("Learning actor was not resolved.");