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 TenantEngagementController(
    ITenantEngagementService engagementService,
    TenantAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("badges")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询租户勋章")]
    [ProducesResponseType<CatalogList<TenantAdminBadgeItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminBadgeItem>>> GetBadges(
        [FromQuery] TenantAdminBadgeQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await engagementService.GetBadgesAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("badges")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("新增或更新租户勋章")]
    [ProducesResponseType<ContentManagementResult<TenantAdminBadgeItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminBadgeItem>>> UpsertBadge(
        UpsertTenantAdminBadgeDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await engagementService.UpsertBadgeAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpGet("badge-grants")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询勋章发放记录")]
    [ProducesResponseType<CatalogList<TenantAdminBadgeGrantItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminBadgeGrantItem>>> GetBadgeGrants(
        [FromQuery] TenantAdminBadgeGrantQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await engagementService.GetBadgeGrantsAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPost("badge-grants")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("向租户成员发放勋章")]
    [ProducesResponseType<ContentManagementResult<TenantAdminBadgeGrantItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminBadgeGrantItem>>> GrantBadge(
        GrantTenantAdminBadgeDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await engagementService.GrantBadgeAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }

    [HttpGet("notifications")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询用户站内通知")]
    [ProducesResponseType<CatalogList<TenantAdminNotificationItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminNotificationItem>>> GetNotifications(
        [FromQuery] TenantAdminNotificationQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await engagementService.GetNotificationsAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPut("notifications")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("新增或更新用户站内通知")]
    [ProducesResponseType<ContentManagementResult<TenantAdminNotificationItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminNotificationItem>>> UpsertNotification(
        UpsertTenantAdminNotificationDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await engagementService.UpsertNotificationAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("feedbacks")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("查询用户反馈")]
    [ProducesResponseType<CatalogList<TenantAdminFeedbackItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CatalogList<TenantAdminFeedbackItem>>> GetFeedbacks(
        [FromQuery] TenantAdminFeedbackQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await engagementService.GetFeedbacksAsync(actorResolver.Resolve(), query.ToFilter(), cancellationToken));
    }

    [HttpPost("feedbacks/status")]
    [Authorize(Policy = BackendPermissions.TenantStudentManage)]
    [EndpointSummary("处理用户反馈")]
    [ProducesResponseType<ContentManagementResult<TenantAdminFeedbackItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ContentManagementResult<TenantAdminFeedbackItem>>> UpdateFeedback(
        UpdateTenantAdminFeedbackDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await engagementService.UpdateFeedbackAsync(actorResolver.Resolve(), request.ToCommand(), cancellationToken));
    }
}

