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 PlatformDunningController(
    IPlatformDunningService service,
    PlatformAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("saas/dunning/channels")]
    [Authorize(Policy = BackendPermissions.PlatformBillingNotification)]
    [EndpointSummary("查询平台催缴通知渠道")]
    [ProducesResponseType<PlatformBillingDunningChannelList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformBillingDunningChannelList>> BillingDunningChannels(
        [FromQuery] PlatformAdminQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetBillingDunningChannelsAsync(actorResolver.Resolve(), query.ToQuery(),
            cancellationToken));
    }

    [HttpPut("saas/dunning/channels")]
    [Authorize(Policy = BackendPermissions.PlatformBillingNotification)]
    [EndpointSummary("创建或更新平台催缴通知渠道")]
    [ProducesResponseType<PlatformBillingDunningChannelItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformBillingDunningChannelItem>> UpsertBillingDunningChannel(
        UpsertPlatformBillingDunningChannelDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertBillingDunningChannelAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("saas/dunning/channels/disable")]
    [Authorize(Policy = BackendPermissions.PlatformBillingNotification)]
    [EndpointSummary("禁用平台催缴通知渠道")]
    [ProducesResponseType<PlatformBillingDunningChannelItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformBillingDunningChannelItem>> DisableBillingDunningChannel(
        DisablePlatformBillingDunningChannelDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.DisableBillingDunningChannelAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("saas/dunning/events")]
    [Authorize(Policy = BackendPermissions.PlatformBillingNotification)]
    [EndpointSummary("查询平台催缴通知事件")]
    [ProducesResponseType<PlatformBillingDunningEventList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformBillingDunningEventList>> BillingDunningEvents(
        [FromQuery] PlatformAdminQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetBillingDunningEventsAsync(actorResolver.Resolve(), query.ToQuery(),
            cancellationToken));
    }

    [HttpGet("saas/dunning/events/detail")]
    [Authorize(Policy = BackendPermissions.PlatformBillingNotification)]
    [EndpointSummary("查询平台催缴通知事件详情")]
    [ProducesResponseType<PlatformBillingDunningEventItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformBillingDunningEventItem>> BillingDunningEventDetail(
        [FromQuery] Guid eventId,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetBillingDunningEventDetailAsync(actorResolver.Resolve(), eventId,
            cancellationToken));
    }

    [HttpPost("saas/dunning/events/retry")]
    [Authorize(Policy = BackendPermissions.PlatformBillingNotification)]
    [EndpointSummary("重新标记平台催缴通知事件待发送")]
    [ProducesResponseType<PlatformBillingDunningEventItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PlatformBillingDunningEventItem>> RetryBillingDunningEvent(
        RetryPlatformBillingDunningEventDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.RetryBillingDunningEventAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("saas/dunning/events/acknowledge")]
    [Authorize(Policy = BackendPermissions.PlatformBillingNotification)]
    [EndpointSummary("人工确认平台催缴通知事件")]
    public async Task<ActionResult<PlatformBillingDunningEventItem>> AcknowledgeBillingDunningEvent(
        ResolvePlatformBillingDunningEventDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.AcknowledgeBillingDunningEventAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("saas/dunning/events/ignore")]
    [Authorize(Policy = BackendPermissions.PlatformBillingNotification)]
    [EndpointSummary("人工忽略平台催缴通知事件")]
    public async Task<ActionResult<PlatformBillingDunningEventItem>> IgnoreBillingDunningEvent(
        ResolvePlatformBillingDunningEventDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.IgnoreBillingDunningEventAsync(actorResolver.Resolve(), request.ToCommand(),
            cancellationToken));
    }
}
