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

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("平台端-审批中心")]
[Route("api/platform/approvals")]
[Authorize(Policy = BackendPermissions.PlatformApprovalView)]
public sealed class PlatformApprovalsController(
    IPlatformApprovalService approvalService,
    ICurrentAccessContext currentAccessContext) : ControllerBase
{
    [HttpGet]
    [EndpointSummary("查询平台审批任务")]
    public async Task<IReadOnlyCollection<PlatformApprovalRequestItem>> Requests(
        PlatformApprovalRequestStatus? status,
        int limit = 100,
        CancellationToken cancellationToken = default)
    {
        return await approvalService.ListAsync(await ActorAsync(cancellationToken), status, limit, cancellationToken);
    }

    [HttpGet("{requestId:guid}")]
    [EndpointSummary("查询平台审批详情")]
    public async Task<PlatformApprovalRequestItem> Details(Guid requestId, CancellationToken cancellationToken)
    {
        return await approvalService.GetAsync(await ActorAsync(cancellationToken), requestId, cancellationToken);
    }

    [HttpGet("policies")]
    [EndpointSummary("查询平台审批策略")]
    public async Task<IReadOnlyCollection<PlatformApprovalPolicyItem>> Policies(CancellationToken cancellationToken)
    {
        return await approvalService.ListPoliciesAsync(await ActorAsync(cancellationToken), cancellationToken);
    }

    [HttpPut("policies/{code}")]
    [Authorize(Policy = BackendPermissions.PlatformApprovalPolicyManage)]
    [EndpointSummary("更新平台审批策略")]
    [PlatformOperationRisk("high", "approval.policy-change")]
    public async Task<PlatformApprovalPolicyItem> UpdatePolicy(
        string code,
        UpdatePlatformApprovalPolicyDto request,
        CancellationToken cancellationToken)
    {
        return await approvalService.UpdatePolicyAsync(await ActorAsync(cancellationToken), request.ToCommand(code),
            cancellationToken);
    }

    [HttpPost("{requestId:guid}/approve")]
    [Authorize(Policy = BackendPermissions.PlatformApprovalDecide)]
    [EndpointSummary("批准并执行平台审批任务")]
    [PlatformOperationRisk("high")]
    public async Task<PlatformApprovalRequestItem> Approve(Guid requestId, PlatformApprovalDecisionDto request,
        CancellationToken cancellationToken)
    {
        return await approvalService.ApproveAsync(await ActorAsync(cancellationToken), requestId, request.Reason,
            cancellationToken);
    }

    [HttpPost("{requestId:guid}/reject")]
    [Authorize(Policy = BackendPermissions.PlatformApprovalDecide)]
    [EndpointSummary("拒绝平台审批任务")]
    [PlatformOperationRisk("high")]
    public async Task<PlatformApprovalRequestItem> Reject(Guid requestId, PlatformApprovalDecisionDto request,
        CancellationToken cancellationToken)
    {
        return await approvalService.RejectAsync(await ActorAsync(cancellationToken), requestId, request.Reason,
            cancellationToken);
    }

    [HttpPost("{requestId:guid}/cancel")]
    [EndpointSummary("撤销本人提交的平台审批任务")]
    public async Task<PlatformApprovalRequestItem> Cancel(Guid requestId, PlatformApprovalDecisionDto request,
        CancellationToken cancellationToken)
    {
        return await approvalService.CancelAsync(await ActorAsync(cancellationToken), requestId, request.Reason,
            cancellationToken);
    }

    private async Task<PlatformApprovalActor> ActorAsync(CancellationToken cancellationToken)
    {
        var access = await currentAccessContext.GetAsync(cancellationToken);
        return access.UserId is { } userId && access.IsUserActive
            ? new PlatformApprovalActor(userId, access.PlatformPermissions)
            : throw new PlatformApprovalException("Platform actor was not resolved.", "platform_access_denied");
    }
}