using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TakeoutSaaS.Application.App.Statistics.Dto;
using TakeoutSaaS.Application.App.Statistics.Queries;
using TakeoutSaaS.Domain.Tenants.Enums;
using TakeoutSaaS.Module.Authorization.Attributes;
using TakeoutSaaS.Shared.Abstractions.Results;
using TakeoutSaaS.Shared.Web.Api;

namespace TakeoutSaaS.AdminApi.Controllers;

/// <summary>
/// 统计数据接口。
/// </summary>
[ApiVersion("1.0")]
[Authorize]
[Route("api/admin/v{version:apiVersion}/statistics")]
public sealed class StatisticsController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 获取订阅概览统计。
    /// </summary>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>订阅概览数据。</returns>
    [HttpGet("subscription-overview")]
    [PermissionAuthorize("statistics:read")]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionOverviewDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<SubscriptionOverviewDto>> GetSubscriptionOverview(CancellationToken cancellationToken)
    {
        var result = await mediator.Send(new GetSubscriptionOverviewQuery(), cancellationToken);
        return ApiResponse<SubscriptionOverviewDto>.Ok(result);
    }

    /// <summary>
    /// 获取配额使用排行。
    /// </summary>
    /// <param name="quotaType">配额类型。</param>
    /// <param name="topN">返回前N条记录，默认10。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>配额使用排行数据。</returns>
    [HttpGet("quota-ranking")]
    [PermissionAuthorize("statistics:read")]
    [ProducesResponseType(typeof(ApiResponse<QuotaUsageRankingDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<QuotaUsageRankingDto>> GetQuotaRanking(
        [FromQuery] TenantQuotaType quotaType,
        [FromQuery] int topN = 10,
        CancellationToken cancellationToken = default)
    {
        var query = new GetQuotaUsageRankingQuery { QuotaType = quotaType, TopN = topN };
        var result = await mediator.Send(query, cancellationToken);
        return ApiResponse<QuotaUsageRankingDto>.Ok(result);
    }

    /// <summary>
    /// 获取收入统计。
    /// </summary>
    /// <param name="monthsCount">统计月份数量，默认12个月。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>收入统计数据。</returns>
    [HttpGet("revenue")]
    [PermissionAuthorize("statistics:read")]
    [ProducesResponseType(typeof(ApiResponse<RevenueStatisticsDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<RevenueStatisticsDto>> GetRevenue(
        [FromQuery] int monthsCount = 12,
        CancellationToken cancellationToken = default)
    {
        var query = new GetRevenueStatisticsQuery { MonthsCount = monthsCount };
        var result = await mediator.Send(query, cancellationToken);
        return ApiResponse<RevenueStatisticsDto>.Ok(result);
    }

    /// <summary>
    /// 获取即将到期的订阅列表。
    /// </summary>
    /// <param name="daysAhead">筛选天数，默认7天内到期。</param>
    /// <param name="onlyWithoutAutoRenew">是否只返回未开启自动续费的订阅。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>即将到期的订阅列表。</returns>
    [HttpGet("expiring-subscriptions")]
    [PermissionAuthorize("statistics:read")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ExpiringSubscriptionDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<ExpiringSubscriptionDto>>> GetExpiringSubscriptions(
        [FromQuery] int daysAhead = 7,
        [FromQuery] bool onlyWithoutAutoRenew = false,
        CancellationToken cancellationToken = default)
    {
        var query = new GetExpiringSubscriptionsQuery
        {
            DaysAhead = daysAhead,
            OnlyWithoutAutoRenew = onlyWithoutAutoRenew
        };
        var result = await mediator.Send(query, cancellationToken);
        return ApiResponse<IReadOnlyList<ExpiringSubscriptionDto>>.Ok(result);
    }
}
