using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TakeoutSaaS.Application.Dictionary.Contracts;
using TakeoutSaaS.Application.Dictionary.Models;
using TakeoutSaaS.Application.Dictionary.Services;
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}/dictionary/groups/{groupId:long}/items")]
public sealed class DictionaryItemsController(
    DictionaryCommandService commandService,
    DictionaryQueryService queryService)
    : BaseApiController
{
    /// <summary>
    /// 查询字典项列表。
    /// </summary>
    [HttpGet]
    [PermissionAuthorize("dictionary:group:read")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<DictionaryItemDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<DictionaryItemDto>>> List(long groupId, CancellationToken cancellationToken)
    {
        var result = await queryService.GetItemsByGroupIdAsync(groupId, cancellationToken);
        return ApiResponse<IReadOnlyList<DictionaryItemDto>>.Ok(result);
    }

    /// <summary>
    /// 创建字典项。
    /// </summary>
    [HttpPost]
    [PermissionAuthorize("dictionary:item:create")]
    [ProducesResponseType(typeof(ApiResponse<DictionaryItemDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<DictionaryItemDto>> Create(long groupId, [FromBody] CreateDictionaryItemRequest request, CancellationToken cancellationToken)
    {
        request.GroupId = groupId;
        var result = await commandService.CreateItemAsync(request, cancellationToken);
        return ApiResponse<DictionaryItemDto>.Ok(result);
    }

    /// <summary>
    /// 更新字典项。
    /// </summary>
    [HttpPut("{itemId:long}")]
    [PermissionAuthorize("dictionary:item:update")]
    [ProducesResponseType(typeof(ApiResponse<DictionaryItemDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<DictionaryItemDto>> Update(long groupId, long itemId, [FromBody] UpdateDictionaryItemRequest request, CancellationToken cancellationToken)
    {
        _ = groupId;
        var result = await commandService.UpdateItemAsync(itemId, request, cancellationToken);
        return ApiResponse<DictionaryItemDto>.Ok(result);
    }

    /// <summary>
    /// 删除字典项。
    /// </summary>
    [HttpDelete("{itemId:long}")]
    [PermissionAuthorize("dictionary:item:delete")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<object>> Delete(long groupId, long itemId, CancellationToken cancellationToken)
    {
        _ = groupId;
        var success = await commandService.DeleteItemAsync(itemId, cancellationToken);
        return success
            ? ApiResponse.Success()
            : ApiResponse.Error(ErrorCodes.NotFound, "字典项不存在");
    }
}
