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

namespace Tiku.Api.Controllers;

[ApiController]
[Tags("学生端-积分")]
[Authorize(Policy = TikuPolicies.CurrentTenantMember)]
[Tiku.Api.Security.RequireSaasFeature(SaasFeatureCatalog.StudentStore)]
[Produces("application/json")]
[Route("api/points")]
public sealed class PointsController(
    IPointService pointService,
    ICurrentUser currentUser,
    ITenantContext currentTenant) : ControllerBase
{
    [HttpGet("summary")]
    [EndpointSummary("查询当前用户积分摘要")]
    [ProducesResponseType<PointSummaryItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PointSummaryItem>> Summary(CancellationToken cancellationToken)
    {
        return Ok(await pointService.GetSummaryAsync(ResolveActor(), cancellationToken));
    }

    [HttpGet("tasks")]
    [EndpointSummary("查询当前可领取积分任务")]
    [ProducesResponseType<PointList<PointTaskItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PointList<PointTaskItem>>> Tasks(
        [FromQuery] PointQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await pointService.GetTasksAsync(
            ResolveActor(),
            query.ToQuery(),
            cancellationToken));
    }

    [HttpPost("tasks/claim")]
    [EndpointSummary("领取积分任务奖励")]
    [ProducesResponseType<PointClaimItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PointClaimItem>> ClaimTask(
        ClaimPointTaskDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await pointService.ClaimTaskAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("exchange-items")]
    [EndpointSummary("查询积分兑换项")]
    [ProducesResponseType<PointList<PointExchangeItemDto>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PointList<PointExchangeItemDto>>> ExchangeItems(
        [FromQuery] PointQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await pointService.GetExchangeItemsAsync(
            ResolveActor(),
            query.ToQuery(),
            cancellationToken));
    }

    [HttpPost("exchange-orders")]
    [EndpointSummary("创建积分兑换订单")]
    [ProducesResponseType<PointExchangeOrderItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PointExchangeOrderItem>> CreateExchangeOrder(
        CreatePointExchangeOrderDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await pointService.CreateExchangeOrderAsync(
            ResolveActor(),
            request.ToCommand(),
            cancellationToken));
    }

    [HttpGet("exchange-orders")]
    [EndpointSummary("查询当前用户积分兑换订单")]
    [ProducesResponseType<PointList<PointExchangeOrderItem>>(StatusCodes.Status200OK)]
    public async Task<ActionResult<PointList<PointExchangeOrderItem>>> ExchangeOrders(
        [FromQuery] PointQueryDto query,
        CancellationToken cancellationToken)
    {
        return Ok(await pointService.GetExchangeOrdersAsync(
            ResolveActor(),
            query.ToQuery(),
            cancellationToken));
    }

    private PointActor ResolveActor()
    {
        if (currentTenant.TenantId is null || currentUser.UserId is null)
        {
            throw new PointException("Current point actor was not resolved.", "point_access_denied");
        }

        return new PointActor(currentTenant.TenantId.Value, currentUser.UserId.Value);
    }
}
