using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using TakeoutSaaS.Application.App.Subscriptions.Commands;
using TakeoutSaaS.Application.App.Subscriptions.Dto;
using TakeoutSaaS.Application.App.Subscriptions.Queries;
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}/subscriptions")]
public sealed class SubscriptionsController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 分页查询订阅列表（支持按状态、套餐、到期时间筛选）。
    /// </summary>
    /// <param name="query">查询条件。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>订阅分页结果。</returns>
    [HttpGet]
    [PermissionAuthorize("subscription:read")]
    [ProducesResponseType(typeof(ApiResponse<PagedResult<SubscriptionListDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<PagedResult<SubscriptionListDto>>> List(
        [FromQuery] GetSubscriptionListQuery query,
        CancellationToken cancellationToken)
    {
        // 1. 查询订阅分页
        var result = await mediator.Send(query, cancellationToken);

        // 2. 返回结果
        return ApiResponse<PagedResult<SubscriptionListDto>>.Ok(result);
    }

    /// <summary>
    /// 查看订阅详情（含套餐信息、配额使用、变更历史）。
    /// </summary>
    /// <param name="subscriptionId">订阅 ID。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>订阅详情或未找到。</returns>
    [HttpGet("{subscriptionId:long}")]
    [PermissionAuthorize("subscription:read")]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<SubscriptionDetailDto>> Detail(
        long subscriptionId,
        CancellationToken cancellationToken)
    {
        // 1. 查询订阅详情
        var result = await mediator.Send(new GetSubscriptionDetailQuery { SubscriptionId = subscriptionId }, cancellationToken);

        // 2. 返回查询结果或 404
        return result is null
            ? ApiResponse<SubscriptionDetailDto>.Error(StatusCodes.Status404NotFound, "订阅不存在")
            : ApiResponse<SubscriptionDetailDto>.Ok(result);
    }

    /// <summary>
    /// 更新订阅基础信息（备注、自动续费等）。
    /// </summary>
    /// <param name="subscriptionId">订阅 ID。</param>
    /// <param name="command">更新命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>更新后的订阅详情或未找到。</returns>
    [HttpPut("{subscriptionId:long}")]
    [PermissionAuthorize("subscription:update")]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<SubscriptionDetailDto>> Update(
        long subscriptionId,
        [FromBody, Required] UpdateSubscriptionCommand command,
        CancellationToken cancellationToken)
    {
        // 1. 绑定路由 ID
        command = command with { SubscriptionId = subscriptionId };

        // 2. 执行更新
        var result = await mediator.Send(command, cancellationToken);

        // 3. 返回更新结果或 404
        return result is null
            ? ApiResponse<SubscriptionDetailDto>.Error(StatusCodes.Status404NotFound, "订阅不存在")
            : ApiResponse<SubscriptionDetailDto>.Ok(result);
    }

    /// <summary>
    /// 延期订阅（增加订阅时长）。
    /// </summary>
    /// <param name="subscriptionId">订阅 ID。</param>
    /// <param name="command">延期命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>延期后的订阅详情或未找到。</returns>
    [HttpPost("{subscriptionId:long}/extend")]
    [PermissionAuthorize("subscription:extend")]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<SubscriptionDetailDto>> Extend(
        long subscriptionId,
        [FromBody, Required] ExtendSubscriptionCommand command,
        CancellationToken cancellationToken)
    {
        // 1. 绑定路由 ID
        command = command with { SubscriptionId = subscriptionId };

        // 2. 执行延期
        var result = await mediator.Send(command, cancellationToken);

        // 3. 返回延期结果或 404
        return result is null
            ? ApiResponse<SubscriptionDetailDto>.Error(StatusCodes.Status404NotFound, "订阅不存在")
            : ApiResponse<SubscriptionDetailDto>.Ok(result);
    }

    /// <summary>
    /// 变更套餐（支持立即生效或下周期生效）。
    /// </summary>
    /// <param name="subscriptionId">订阅 ID。</param>
    /// <param name="command">变更套餐命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>变更后的订阅详情或未找到。</returns>
    [HttpPost("{subscriptionId:long}/change-plan")]
    [PermissionAuthorize("subscription:change-plan")]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<SubscriptionDetailDto>> ChangePlan(
        long subscriptionId,
        [FromBody, Required] ChangeSubscriptionPlanCommand command,
        CancellationToken cancellationToken)
    {
        // 1. 绑定路由 ID
        command = command with { SubscriptionId = subscriptionId };

        // 2. 执行套餐变更
        var result = await mediator.Send(command, cancellationToken);

        // 3. 返回变更结果或 404
        return result is null
            ? ApiResponse<SubscriptionDetailDto>.Error(StatusCodes.Status404NotFound, "订阅不存在")
            : ApiResponse<SubscriptionDetailDto>.Ok(result);
    }

    /// <summary>
    /// 变更订阅状态。
    /// </summary>
    /// <param name="subscriptionId">订阅 ID。</param>
    /// <param name="command">状态变更命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>变更后的订阅详情或未找到。</returns>
    [HttpPost("{subscriptionId:long}/status")]
    [PermissionAuthorize("subscription:update-status")]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<SubscriptionDetailDto>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<SubscriptionDetailDto>> UpdateStatus(
        long subscriptionId,
        [FromBody, Required] UpdateSubscriptionStatusCommand command,
        CancellationToken cancellationToken)
    {
        // 1. 绑定路由 ID
        command = command with { SubscriptionId = subscriptionId };

        // 2. 执行状态变更
        var result = await mediator.Send(command, cancellationToken);

        // 3. 返回变更结果或 404
        return result is null
            ? ApiResponse<SubscriptionDetailDto>.Error(StatusCodes.Status404NotFound, "订阅不存在")
            : ApiResponse<SubscriptionDetailDto>.Ok(result);
    }

    /// <summary>
    /// 批量延期订阅。
    /// </summary>
    /// <param name="command">批量延期命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>批量延期结果。</returns>
    [HttpPost("batch-extend")]
    [PermissionAuthorize("subscription:batch-extend")]
    [ProducesResponseType(typeof(ApiResponse<BatchExtendResult>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<BatchExtendResult>> BatchExtend(
        [FromBody, Required] BatchExtendSubscriptionsCommand command,
        CancellationToken cancellationToken)
    {
        var result = await mediator.Send(command, cancellationToken);
        return ApiResponse<BatchExtendResult>.Ok(result);
    }

    /// <summary>
    /// 批量发送续费提醒。
    /// </summary>
    /// <param name="command">批量发送提醒命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>批量发送提醒结果。</returns>
    [HttpPost("batch-remind")]
    [PermissionAuthorize("subscription:batch-remind")]
    [ProducesResponseType(typeof(ApiResponse<BatchSendReminderResult>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<BatchSendReminderResult>> BatchRemind(
        [FromBody, Required] BatchSendReminderCommand command,
        CancellationToken cancellationToken)
    {
        var result = await mediator.Send(command, cancellationToken);
        return ApiResponse<BatchSendReminderResult>.Ok(result);
    }
}
