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 PointAdministrationController(
    IPointAdministrationService service,
    CommerceAdminActorResolver actorResolver) : ControllerBase
{
    [HttpGet("point-activity-tasks")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询积分活动任务")]
    [ProducesResponseType<TenantPointTaskList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantPointTaskList>> PointTasks(
        [FromQuery] TenantCommerceQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetPointTasksAsync(
            actorResolver.Resolve(),
            actorResolver.ToPointQuery(query),
            cancellationToken));
    }

    [HttpPut("point-activity-tasks")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("新增或更新积分活动任务")]
    [ProducesResponseType<object>(StatusCodes.Status200OK)]
    public async Task<ActionResult<object>> UpsertPointTask(
        UpsertPointTaskDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertPointTaskAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("point-activity-claims")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询积分任务领取记录")]
    [ProducesResponseType<TenantPointClaimList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantPointClaimList>> PointClaims(
        [FromQuery] TenantCommerceQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetPointClaimsAsync(
            actorResolver.Resolve(),
            actorResolver.ToPointQuery(query),
            cancellationToken));
    }

    [HttpGet("point-exchange-items")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询积分兑换项")]
    [ProducesResponseType<TenantPointExchangeItemList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantPointExchangeItemList>> PointExchangeItems(
        [FromQuery] TenantCommerceQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetPointExchangeItemsAsync(
            actorResolver.Resolve(),
            actorResolver.ToPointQuery(query),
            cancellationToken));
    }

    [HttpPut("point-exchange-items")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("新增或更新积分兑换项")]
    [ProducesResponseType<object>(StatusCodes.Status200OK)]
    public async Task<ActionResult<object>> UpsertPointExchangeItem(
        UpsertPointExchangeItemDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpsertPointExchangeItemAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("point-exchange-orders")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("查询积分兑换订单")]
    [ProducesResponseType<TenantPointExchangeOrderList>(StatusCodes.Status200OK)]
    public async Task<ActionResult<TenantPointExchangeOrderList>> PointExchangeOrders(
        [FromQuery] TenantCommerceQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await service.GetPointExchangeOrdersAsync(
            actorResolver.Resolve(),
            actorResolver.ToPointQuery(query),
            cancellationToken));
    }

    [HttpPost("point-exchange-orders/status")]
    [Authorize(Policy = TikuPolicies.TenantCommerceOperateAllScope)]
    [EndpointSummary("更新积分兑换订单状态")]
    [ProducesResponseType<object>(StatusCodes.Status200OK)]
    public async Task<ActionResult<object>> UpdatePointExchangeOrderStatus(
        UpdatePointExchangeOrderStatusDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await service.UpdatePointExchangeOrderStatusAsync(
            actorResolver.Resolve(),
            request.ToCommand(),
            cancellationToken));
    }
}
