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 TenantStudentEngagementController(
    ITenantStudentEngagementService studentEngagementService,
    TenantAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("student-notes")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询学生备注")]
    [ProducesResponseType<CatalogList<TenantAdminStudentNoteItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminStudentNoteItem>>> GetStudentNotes(
        [FromQuery] TenantAdminStudentActivityQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await studentEngagementService.GetStudentNotesAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("student-notes")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("新增或更新学生备注")]
    [ProducesResponseType<ContentManagementResult<TenantAdminStudentNoteItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminStudentNoteItem>>> UpsertStudentNote(
        UpsertTenantAdminStudentNoteDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await studentEngagementService.UpsertStudentNoteAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("student-followups")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询学生跟进")]
    [ProducesResponseType<CatalogList<TenantAdminStudentFollowupItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminStudentFollowupItem>>> GetStudentFollowups(
        [FromQuery] TenantAdminStudentActivityQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(
            await studentEngagementService.GetStudentFollowupsAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("student-followups")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("新增或更新学生跟进")]
    [ProducesResponseType<ContentManagementResult<TenantAdminStudentFollowupItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminStudentFollowupItem>>> UpsertStudentFollowup(
        UpsertTenantAdminStudentFollowupDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await studentEngagementService.UpsertStudentFollowupAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }
}

