using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Application.PlatformAdmin;
using Tiku.Application.Security;
using Tiku.Application.Tenancy;
using Tiku.Domain.Growth;
using Tiku.Domain.Tenancy;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("平台端-租户 CRM 能力")]
[Authorize(Policy = TikuPolicies.PlatformBackofficeBootstrap)]
[Produces("application/json")]
[Route("api/platform/tenant-capabilities/crm")]
public sealed class PlatformAdminCrmController(
    IPlatformCrmAdminService crmService,
    ICurrentUser currentUser) : ControllerBase
{
    [HttpGet("configs")]
    [EndpointSummary("查询租户 CRM 配置")]
    [Authorize(Policy = BackendPermissions.PlatformCrmRead)]
    public Task<PlatformTenantCapabilityList<PlatformCrmConfigItem>> Configs(
        [FromQuery] PlatformCapabilityQueryDto query, CancellationToken cancellationToken)
    {
        return crmService.GetConfigsAsync(Actor(), query.ToQuery(), cancellationToken);
    }

    [HttpPut("configs")]
    [EndpointSummary("新增或更新租户 CRM 配置")]
    [Authorize(Policy = BackendPermissions.PlatformCrmWrite)]
    public Task<PlatformCrmConfigItem> UpsertConfig(UpsertPlatformCrmConfigDto request,
        CancellationToken cancellationToken)
    {
        return crmService.UpsertConfigAsync(Actor(), request.ToCommand(), cancellationToken);
    }

    [HttpGet("leads")]
    [EndpointSummary("查询租户 CRM 线索队列")]
    [Authorize(Policy = BackendPermissions.PlatformCrmRead)]
    public Task<PlatformTenantCapabilityList<CrmWebhookQueueItem>> Leads([FromQuery] PlatformCapabilityQueryDto query,
        CancellationToken cancellationToken)
    {
        return crmService.GetLeadsAsync(Actor(), query.ToQuery(), cancellationToken);
    }

    [HttpPost("leads/retry")]
    [EndpointSummary("重试租户 CRM 线索推送")]
    [Authorize(Policy = BackendPermissions.PlatformCrmWrite)]
    public Task<CrmWebhookQueueItem> RetryLead(RetryPlatformCrmLeadDto request, CancellationToken cancellationToken)
    {
        return crmService.RetryLeadAsync(Actor(), request.ToCommand(), cancellationToken);
    }

    [HttpGet("logs")]
    [EndpointSummary("查询租户 CRM 推送日志")]
    [Authorize(Policy = BackendPermissions.PlatformCrmRead)]
    public Task<PlatformTenantCapabilityList<CrmWebhookLog>> Logs([FromQuery] PlatformCrmLogQueryDto query,
        CancellationToken cancellationToken)
    {
        return crmService.GetLogsAsync(Actor(), query.TenantId, query.QueueId, query.Limit, cancellationToken);
    }

    private PlatformCapabilityActor Actor()
    {
        return currentUser.UserId is { } userId
            ? new PlatformCapabilityActor(userId)
            : throw new PlatformCapabilityException("Platform actor was not resolved.", "platform_access_denied");
    }
}

[ApiController]
[Tags("平台端-租户短信能力")]
[Authorize(Policy = TikuPolicies.PlatformBackofficeBootstrap)]
[Produces("application/json")]
[Route("api/platform/tenant-capabilities/sms")]
public sealed class PlatformAdminSmsController(
    IPlatformSmsAdminService smsService,
    ICurrentUser currentUser) : ControllerBase
{
    [HttpGet("channels")]
    [EndpointSummary("查询租户短信渠道")]
    [Authorize(Policy = BackendPermissions.PlatformSmsRead)]
    public Task<PlatformTenantCapabilityList<PlatformSmsChannelItem>> Channels(
        [FromQuery] PlatformCapabilityQueryDto query, CancellationToken cancellationToken)
    {
        return smsService.GetChannelsAsync(Actor(), query.ToQuery(), cancellationToken);
    }

    [HttpPut("channels")]
    [EndpointSummary("新增或更新租户短信渠道")]
    [Authorize(Policy = BackendPermissions.PlatformSmsWrite)]
    public Task<PlatformSmsChannelItem> UpsertChannel(UpsertPlatformSmsChannelDto request,
        CancellationToken cancellationToken)
    {
        return smsService.UpsertChannelAsync(Actor(), request.ToCommand(), cancellationToken);
    }

    [HttpPost("channels/{id:guid}/disable")]
    [EndpointSummary("禁用租户短信渠道")]
    [Authorize(Policy = BackendPermissions.PlatformSmsWrite)]
    public Task<PlatformSmsChannelItem> DisableChannel(Guid id, CancellationToken cancellationToken)
    {
        return smsService.DisableChannelAsync(Actor(), id, cancellationToken);
    }

    [HttpGet("templates")]
    [EndpointSummary("查询租户短信模板")]
    [Authorize(Policy = BackendPermissions.PlatformSmsRead)]
    public Task<PlatformTenantCapabilityList<PlatformSmsTemplateItem>> Templates(
        [FromQuery] PlatformCapabilityQueryDto query, CancellationToken cancellationToken)
    {
        return smsService.GetTemplatesAsync(Actor(), query.ToQuery(), cancellationToken);
    }

    [HttpPut("templates")]
    [EndpointSummary("新增或更新租户短信模板")]
    [Authorize(Policy = BackendPermissions.PlatformSmsWrite)]
    public Task<PlatformSmsTemplateItem> UpsertTemplate(UpsertPlatformSmsTemplateDto request,
        CancellationToken cancellationToken)
    {
        return smsService.UpsertTemplateAsync(Actor(), request.ToCommand(), cancellationToken);
    }

    [HttpPost("templates/{id:guid}/submit-review")]
    [EndpointSummary("提交租户短信模板审核")]
    [Authorize(Policy = BackendPermissions.PlatformSmsWrite)]
    public Task<PlatformSmsTemplateItem> SubmitTemplateReview(Guid id, CancellationToken cancellationToken)
    {
        return smsService.SubmitTemplateReviewAsync(Actor(), id, cancellationToken);
    }

    [HttpPost("templates/{id:guid}/disable")]
    [EndpointSummary("禁用租户短信模板")]
    [Authorize(Policy = BackendPermissions.PlatformSmsWrite)]
    public Task<PlatformSmsTemplateItem> DisableTemplate(Guid id, CancellationToken cancellationToken)
    {
        return smsService.DisableTemplateAsync(Actor(), id, cancellationToken);
    }

    [HttpGet("logs")]
    [EndpointSummary("查询租户短信发送日志")]
    [Authorize(Policy = BackendPermissions.PlatformSmsRead)]
    public Task<PlatformTenantCapabilityList<SmsSendLog>> Logs([FromQuery] PlatformCapabilityQueryDto query,
        CancellationToken cancellationToken)
    {
        return smsService.GetLogsAsync(Actor(), query.ToQuery(), cancellationToken);
    }

    private PlatformCapabilityActor Actor()
    {
        return currentUser.UserId is { } userId
            ? new PlatformCapabilityActor(userId)
            : throw new PlatformCapabilityException("Platform actor was not resolved.", "platform_access_denied");
    }
}

[ApiController]
[Tags("平台端-租户支付能力")]
[Authorize(Policy = TikuPolicies.PlatformBackofficeBootstrap)]
[Produces("application/json")]
[Route("api/platform/tenant-capabilities/payments")]
public sealed class PlatformAdminTenantPaymentSettingsController(
    IPlatformTenantPaymentAdminService paymentService,
    ICurrentUser currentUser) : ControllerBase
{
    [HttpGet("apps")]
    [EndpointSummary("查询租户支付应用")]
    [Authorize(Policy = BackendPermissions.PlatformPaymentRead)]
    public Task<PlatformTenantCapabilityList<TenantExternalProviderItem>> Apps(
        [FromQuery] PlatformCapabilityQueryDto query, CancellationToken cancellationToken)
    {
        return paymentService.GetAppsAsync(Actor(), query.ToQuery(), cancellationToken);
    }

    [HttpPut("apps")]
    [EndpointSummary("新增或更新租户支付应用")]
    [Authorize(Policy = BackendPermissions.PlatformPaymentWrite)]
    public Task<TenantExternalProviderItem> UpsertApp(UpsertPlatformTenantPaymentAppDto request,
        CancellationToken cancellationToken)
    {
        return paymentService.UpsertAppAsync(Actor(), request.TenantId, request.ToCommand(), cancellationToken);
    }

    [HttpGet("events")]
    [EndpointSummary("查询租户支付事件")]
    [Authorize(Policy = BackendPermissions.PlatformPaymentRead)]
    public Task<PlatformTenantCapabilityList<object>> Events([FromQuery] PlatformCapabilityQueryDto query,
        CancellationToken cancellationToken)
    {
        return paymentService.GetEventsAsync(Actor(), query.ToQuery(), cancellationToken);
    }

    private PlatformCapabilityActor Actor()
    {
        return currentUser.UserId is { } userId
            ? new PlatformCapabilityActor(userId)
            : throw new PlatformCapabilityException("Platform actor was not resolved.", "platform_access_denied");
    }
}