using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using TakeoutSaaS.Application.App.Tenants.Commands;
using TakeoutSaaS.Application.App.Tenants.Dto;
using TakeoutSaaS.Application.App.Tenants.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}/tenant-packages")]
public sealed class TenantPackagesController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 分页查询租户套餐。
    /// </summary>
    /// <param name="query">查询条件。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>租户套餐分页结果。</returns>
    [HttpGet]
    [PermissionAuthorize("tenant-package:read")]
    [ProducesResponseType(typeof(ApiResponse<PagedResult<TenantPackageDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<PagedResult<TenantPackageDto>>> Search([FromQuery] SearchTenantPackagesQuery query, CancellationToken cancellationToken)
    {
        // 1. 查询租户套餐分页
        var result = await mediator.Send(query, cancellationToken);

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

    /// <summary>
    /// 查看套餐详情。
    /// </summary>
    /// <param name="tenantPackageId">套餐 ID。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>套餐详情或未找到。</returns>
    [HttpGet("{tenantPackageId:long}")]
    [PermissionAuthorize("tenant-package:read")]
    [ProducesResponseType(typeof(ApiResponse<TenantPackageDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<TenantPackageDto>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<TenantPackageDto>> Detail(long tenantPackageId, CancellationToken cancellationToken)
    {
        // 1. 查询套餐详情
        var result = await mediator.Send(new GetTenantPackageByIdQuery { TenantPackageId = tenantPackageId }, cancellationToken);

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

    /// <summary>
    /// 创建套餐。
    /// </summary>
    /// <param name="command">创建命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>创建后的套餐。</returns>
    [HttpPost]
    [PermissionAuthorize("tenant-package:create")]
    [ProducesResponseType(typeof(ApiResponse<TenantPackageDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<TenantPackageDto>> Create([FromBody, Required] CreateTenantPackageCommand command, CancellationToken cancellationToken)
    {
        // 1. 执行创建
        var result = await mediator.Send(command, cancellationToken);

        // 2. 返回创建结果
        return ApiResponse<TenantPackageDto>.Ok(result);
    }

    /// <summary>
    /// 更新套餐。
    /// </summary>
    /// <param name="tenantPackageId">套餐 ID。</param>
    /// <param name="command">更新命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>更新后的套餐或未找到。</returns>
    [HttpPut("{tenantPackageId:long}")]
    [PermissionAuthorize("tenant-package:update")]
    [ProducesResponseType(typeof(ApiResponse<TenantPackageDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<TenantPackageDto>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<TenantPackageDto>> Update(long tenantPackageId, [FromBody, Required] UpdateTenantPackageCommand command, CancellationToken cancellationToken)
    {
        // 1. 绑定路由 ID
        command = command with { TenantPackageId = tenantPackageId };

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

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

    /// <summary>
    /// 删除套餐。
    /// </summary>
    /// <param name="tenantPackageId">套餐 ID。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>删除结果。</returns>
    [HttpDelete("{tenantPackageId:long}")]
    [PermissionAuthorize("tenant-package:delete")]
    [ProducesResponseType(typeof(ApiResponse<bool>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<bool>> Delete(long tenantPackageId, CancellationToken cancellationToken)
    {
        // 1. 构建删除命令
        var command = new DeleteTenantPackageCommand { TenantPackageId = tenantPackageId };

        // 2. 执行删除并返回
        var result = await mediator.Send(command, cancellationToken);
        return ApiResponse<bool>.Ok(result);
    }
}
