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 CommerceAdjustmentController(
    ICommerceAdjustmentService service,
    CommerceAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("adjustment-vouchers")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询调账凭证")]
    [ProducesResponseType<TenantAdjustmentVoucherList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdjustmentVoucherList>> AdjustmentVouchers(
        [FromQuery] TenantCommerceQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetAdjustmentVouchersAsync(
            actorResolver.Resolve(),
            actorResolver.ToQuery(query),
            cancellationToken));
    }

    [HttpGet("adjustment-vouchers/detail")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询调账凭证详情")]
    [ProducesResponseType<CommerceAdjustmentVoucher>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CommerceAdjustmentVoucher>> AdjustmentVoucherDetail(
        [FromQuery] Guid voucherId,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetAdjustmentVoucherAsync(
            actorResolver.Resolve(),
            voucherId,
            cancellationToken));
    }

    [HttpPost("adjustment-vouchers")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("创建调账凭证")]
    [ProducesResponseType<CommerceAdjustmentVoucher>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CommerceAdjustmentVoucher>> CreateAdjustmentVoucher(
        CreateAdjustmentVoucherDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.CreateAdjustmentVoucherAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpPost("adjustment-vouchers/status")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("审核或关闭调账凭证")]
    [ProducesResponseType<CommerceAdjustmentVoucher>(StatusCodes.Status200OK)]
    public async Task<ActionResult<CommerceAdjustmentVoucher>> UpdateAdjustmentVoucherStatus(
        UpdateAdjustmentVoucherStatusDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpdateAdjustmentVoucherStatusAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("adjustment-vouchers/events")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询调账凭证事件")]
    [ProducesResponseType<TenantAdjustmentVoucherEventList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdjustmentVoucherEventList>> AdjustmentVoucherEvents(
        [FromQuery] Guid voucherId,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetAdjustmentVoucherEventsAsync(
            actorResolver.Resolve(),
            voucherId,
            cancellationToken));
    }

    [HttpGet("adjustment-vouchers/report")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询调账统计报告")]
    [ProducesResponseType<TenantAdjustmentReport>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantAdjustmentReport>> AdjustmentVoucherReport(CancellationToken cancellationToken)
    {
        return Ok(await service.GetAdjustmentReportAsync(
            actorResolver.Resolve(),
            cancellationToken));
    }

    [HttpGet("reconciliation/anomalies")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询对账异常汇总")]
    [ProducesResponseType<TenantCommerceAnomalySummary>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantCommerceAnomalySummary>> ReconciliationAnomalies(
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetAnomalySummaryAsync(
            actorResolver.Resolve(),
            cancellationToken));
    }
}
