using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TakeoutSaaS.Application.App.Merchants.Commands;
using TakeoutSaaS.Application.App.Merchants.Dto;
using TakeoutSaaS.Application.App.Merchants.Queries;
using TakeoutSaaS.Module.Authorization.Attributes;
using TakeoutSaaS.Shared.Abstractions.Constants;
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}/merchant-categories")]
public sealed class MerchantCategoriesController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 列出所有类目。
    /// </summary>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>类目列表。</returns>
    [HttpGet]
    [PermissionAuthorize("merchant_category:read")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<MerchantCategoryDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<MerchantCategoryDto>>> List(CancellationToken cancellationToken)
    {
        // 1. 查询所有类目
        var result = await mediator.Send(new ListMerchantCategoriesQuery(), cancellationToken);

        // 2. 返回类目列表
        return ApiResponse<IReadOnlyList<MerchantCategoryDto>>.Ok(result);
    }

    /// <summary>
    /// 新增类目。
    /// </summary>
    /// <param name="command">创建命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>创建的类目。</returns>
    [HttpPost]
    [PermissionAuthorize("merchant_category:create")]
    [ProducesResponseType(typeof(ApiResponse<MerchantCategoryDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<MerchantCategoryDto>> Create([FromBody] CreateMerchantCategoryCommand command, CancellationToken cancellationToken)
    {
        // 1. 创建类目
        var result = await mediator.Send(command, cancellationToken);

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

    /// <summary>
    /// 删除类目。
    /// </summary>
    /// <param name="categoryId">类目 ID。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>删除结果，未找到则返回错误。</returns>
    [HttpDelete("{categoryId:long}")]
    [PermissionAuthorize("merchant_category:delete")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<object>> Delete(long categoryId, CancellationToken cancellationToken)
    {
        // 1. 执行删除
        var success = await mediator.Send(new DeleteMerchantCategoryCommand(categoryId), cancellationToken);

        // 2. 返回删除结果或 404
        return success
            ? ApiResponse<object>.Ok(null)
            : ApiResponse<object>.Error(ErrorCodes.NotFound, "类目不存在");
    }

    /// <summary>
    /// 批量调整类目排序。
    /// </summary>
    /// <param name="command">排序命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>执行结果。</returns>
    [HttpPost("reorder")]
    [PermissionAuthorize("merchant_category:update")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<object>> Reorder([FromBody] ReorderMerchantCategoriesCommand command, CancellationToken cancellationToken)
    {
        // 1. 执行排序调整
        await mediator.Send(command, cancellationToken);

        // 2. 返回成功结果
        return ApiResponse<object>.Ok(null);
    }
}
