using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Application.Auth;
using Tiku.Application.Catalog;
using Tiku.Application.Content;
using Tiku.Application.Security;
using Tiku.Application.TenantAdmin;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("租户端-运营管理")]
[Produces("application/json")]
[Route("api/tenant")]
public sealed class TenantSupervisionController(
    ITenantSupervisionService supervisionService,
    TenantAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("students/supervision/rules")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询学习督导规则")]
    [ProducesResponseType<CatalogList<TenantSupervisionRuleItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantSupervisionRuleItem>>> SupervisionRules(
        CancellationToken cancellationToken)
    {
        return Ok(await supervisionService.GetSupervisionRulesAsync(actorResolver.Resolve(), cancellationToken));
    }

    [HttpPut("students/supervision/rules")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("新增或更新学习督导规则")]
    [ProducesResponseType<ContentManagementResult<TenantSupervisionRuleItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantSupervisionRuleItem>>> UpsertSupervisionRule(
        UpsertTenantSupervisionRuleDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await supervisionService.UpsertSupervisionRuleAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("students/supervision/preview")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("预览学习风险学生")]
    [ProducesResponseType<TenantSupervisionPreview>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantSupervisionPreview>> PreviewSupervision(CancellationToken cancellationToken)
    {
        return Ok(await supervisionService.PreviewSupervisionAsync(actorResolver.Resolve(), cancellationToken));
    }

    [HttpPost("students/supervision/generate")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("批量生成学习督导跟进任务")]
    [ProducesResponseType<TenantSupervisionGenerateResult>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantSupervisionGenerateResult>> GenerateSupervision(
        TenantSupervisionGenerateDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await supervisionService.GenerateSupervisionFollowupsAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("student-followups/report")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询学生跟进统计报表")]
    [ProducesResponseType<TenantFollowupReport>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantFollowupReport>> FollowupReport(CancellationToken cancellationToken)
    {
        return Ok(await supervisionService.GetFollowupReportAsync(actorResolver.Resolve(), cancellationToken));
    }

    [HttpGet("feedbacks/report")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询反馈处理统计报表")]
    [ProducesResponseType<TenantFeedbackReport>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantFeedbackReport>> FeedbackReport(CancellationToken cancellationToken)
    {
        return Ok(await supervisionService.GetFeedbackReportAsync(actorResolver.Resolve(), cancellationToken));
    }

    [HttpGet("points/risk-report")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询积分风险统计报表")]
    [ProducesResponseType<TenantPointRiskReport>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantPointRiskReport>> PointRiskReport(CancellationToken cancellationToken)
    {
        return Ok(await supervisionService.GetPointRiskReportAsync(actorResolver.Resolve(), cancellationToken));
    }
}

