using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Api.Security;
using Tiku.Application.Commerce;
using Tiku.Application.Jobs;
using Tiku.Application.Points;
using Tiku.Application.Security;
using Tiku.Domain.Commerce;

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("租户端-交易运营")]
[Authorize(Policy = BackendPermissions.TenantCommerceOperate)]
[RequireSaasFeature(SaasFeatureCatalog.StudentStore)]
[Produces("application/json")]
[Route("api/tenant/commerce")]
public sealed class ReconciliationAdministrationController(
    IReconciliationAdministrationService service,
    CommerceAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("reconciliation/batches")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询对账批次")]
    [ProducesResponseType<TenantReconciliationBatchList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantReconciliationBatchList>> ReconciliationBatches(
        [FromQuery] TenantCommerceQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetReconciliationBatchesAsync(
            actorResolver.Resolve(),
            actorResolver.ToQuery(query),
            cancellationToken));
    }

    [HttpPost("reconciliation/batches")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("创建对账批次")]
    [ProducesResponseType<CommerceReconciliationBatch>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CommerceReconciliationBatch>> CreateReconciliationBatch(
        CreateReconciliationBatchDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.CreateReconciliationBatchAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("reconciliation/issues")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询对账异常")]
    [ProducesResponseType<TenantReconciliationIssueList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantReconciliationIssueList>> ReconciliationIssues(
        [FromQuery] TenantCommerceQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetReconciliationIssuesAsync(
            actorResolver.Resolve(),
            actorResolver.ToQuery(query),
            cancellationToken));
    }

    [HttpGet("reconciliation/items")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询对账明细")]
    [ProducesResponseType<TenantReconciliationItemList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantReconciliationItemList>> ReconciliationItems(
        [FromQuery] Guid batchId,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetReconciliationItemsAsync(
            actorResolver.Resolve(),
            batchId,
            cancellationToken));
    }

    [HttpGet("reconciliation/issues/events")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询对账工单事件")]
    [ProducesResponseType<TenantReconciliationIssueEventList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantReconciliationIssueEventList>> ReconciliationIssueEvents(
        [FromQuery] Guid issueId,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetReconciliationIssueEventsAsync(
            actorResolver.Resolve(),
            issueId,
            cancellationToken));
    }

    [HttpPost("reconciliation/issues/status")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("更新对账异常状态")]
    [ProducesResponseType<CommerceReconciliationIssue>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CommerceReconciliationIssue>> UpdateReconciliationIssue(
        UpdateReconciliationIssueDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpdateReconciliationIssueAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("reconciliation/preview")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("预览支付渠道账单对账")]
    [ProducesResponseType<ReconciliationImportPreview>(StatusCodes.Status200OK)]
    public async Task<ActionResult<ReconciliationImportPreview>> PreviewReconciliation(
        PreviewReconciliationImportDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.PreviewReconciliationImportAsync(
            actorResolver.Resolve(),
            request.ToPreviewCommand(),
            cancellationToken));
    }

    [HttpPost("reconciliation/import")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("导入支付渠道账单并创建对账批次")]
    [ProducesResponseType<CommerceReconciliationBatch>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CommerceReconciliationBatch>> ImportReconciliation(
        PreviewReconciliationImportDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.ImportReconciliationAsync(
            actorResolver.Resolve(),
            request.ToImportCommand(),
            cancellationToken));
    }

    [HttpGet("reconciliation/provider-bills/jobs")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询渠道账单下载任务")]
    [ProducesResponseType<IReadOnlyCollection<BackgroundJobItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<IReadOnlyCollection<BackgroundJobItem>>> ProviderBillJobs(
        [FromQuery] TenantCommerceQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetProviderBillJobsAsync(
            actorResolver.Resolve(),
            actorResolver.ToQuery(query),
            cancellationToken));
    }

    [HttpPost("reconciliation/provider-bills/request")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("创建渠道官方账单下载任务")]
    [ProducesResponseType<BackgroundJobItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackgroundJobItem>> RequestProviderBill(
        RequestProviderBillJobDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.RequestProviderBillJobAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }
}
