using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TakeoutSaaS.Application.Dictionary.Models;
using TakeoutSaaS.Application.Dictionary.Services;
using TakeoutSaaS.Domain.Dictionary.Enums;
using TakeoutSaaS.Module.Authorization.Attributes;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Results;
using TakeoutSaaS.Shared.Abstractions.Security;
using TakeoutSaaS.Shared.Web.Api;

namespace TakeoutSaaS.AdminApi.Controllers;

/// <summary>
/// 字典标签覆盖管理（平台端）。
/// </summary>
[ApiVersion("1.0")]
[Authorize]
[Route("api/admin/v{version:apiVersion}/dictionary/label-overrides")]
public sealed class DictionaryLabelOverridesController(
    DictionaryLabelOverrideService labelOverrideService,
    ICurrentUserAccessor currentUserAccessor)
    : BaseApiController
{
    /// <summary>
    /// 获取指定租户的所有标签覆盖（平台管理员用）。
    /// </summary>
    [HttpGet("platform/{targetTenantId:long}")]
    [PermissionAuthorize("dictionary:override:platform:read")]
    [ProducesResponseType(typeof(ApiResponse<IReadOnlyList<LabelOverrideDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<IReadOnlyList<LabelOverrideDto>>> ListPlatformOverrides(
        long targetTenantId,
        [FromQuery] OverrideType? overrideType,
        CancellationToken cancellationToken)
    {
        var result = await labelOverrideService.GetOverridesAsync(targetTenantId, overrideType, cancellationToken);
        return ApiResponse<IReadOnlyList<LabelOverrideDto>>.Ok(result);
    }

    /// <summary>
    /// 平台强制覆盖租户字典项的标签。
    /// </summary>
    [HttpPost("platform/{targetTenantId:long}")]
    [PermissionAuthorize("dictionary:override:platform:update")]
    [ProducesResponseType(typeof(ApiResponse<LabelOverrideDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<LabelOverrideDto>> CreatePlatformOverride(
        long targetTenantId,
        [FromBody] UpsertLabelOverrideRequest request,
        CancellationToken cancellationToken)
    {
        var operatorId = currentUserAccessor.UserId;
        var result = await labelOverrideService.UpsertPlatformOverrideAsync(targetTenantId, request, operatorId, cancellationToken);
        return ApiResponse<LabelOverrideDto>.Ok(result);
    }

    /// <summary>
    /// 平台删除对租户的强制覆盖。
    /// </summary>
    [HttpDelete("platform/{targetTenantId:long}/{dictionaryItemId:long}")]
    [PermissionAuthorize("dictionary:override:platform:delete")]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<object>> DeletePlatformOverride(
        long targetTenantId,
        long dictionaryItemId,
        CancellationToken cancellationToken)
    {
        var operatorId = currentUserAccessor.UserId;
        var success = await labelOverrideService.DeleteOverrideAsync(
            targetTenantId,
            dictionaryItemId,
            operatorId,
            cancellationToken: cancellationToken);
        return success
            ? ApiResponse.Success()
            : ApiResponse.Error(ErrorCodes.NotFound, "覆盖配置不存在");
    }
}
