using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Api.OpenApi;
using Tiku.Application.Auth;
using Tiku.Application.PlatformAdmin;
using Tiku.Application.Security;
using Tiku.Domain.Platform;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("平台端-平台管理")]
[Authorize(Policy = BackendPermissions.PlatformDashboardView)]
[Produces("application/json")]
[Route("api/platform")]
public sealed class PlatformAuditAlertController(
    IPlatformAuditAlertService service,
    PlatformAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("audit-logs")]
    [Authorize(Policy = BackendPermissions.PlatformAuditView)]
    [EndpointSummary("查询平台审计日志")]
    [ProducesResponseType<PlatformAuditLogList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformAuditLogList>> AuditLogs(
        [FromQuery] PlatformAdminQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetAuditLogsAsync(actorResolver.Resolve(), query.ToQuery(), cancellationToken));
    }

    [HttpGet("audit-alerts")]
    [Authorize(Policy = BackendPermissions.PlatformAuditView)]
    [EndpointSummary("查询平台审计告警")]
    [ProducesResponseType<PlatformAuditAlertList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformAuditAlertList>> AuditAlerts(
        [FromQuery] PlatformAdminQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetAuditAlertsAsync(actorResolver.Resolve(), query.ToQuery(), cancellationToken));
    }

    [HttpPost("audit-alerts/status")]
    [Authorize(Policy = BackendPermissions.PlatformAuditView)]
    [EndpointSummary("更新平台审计告警状态")]
    [ProducesResponseType<PlatformAuditAlert>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformAuditAlert>> AuditAlertStatus(
        UpdatePlatformAuditAlertStatusDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpdateAuditAlertStatusAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }
}
