namespace WebApi.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class ConfigController(
    IConfigService configService
) : ControllerBase
{
    /// <summary>
    /// 获取配置值
    /// </summary>
    public async Task<BaseResponse<string>> GetConfig(string key)
    {
        try
        {
            var config = await configService.GetConfigByKeyAsync<string>(key);
            var value = config ?? string.Empty;
            return value.Ok("配置获取成功");
        }
        catch (Exception ex)
        {
            return $"获取{key}配置失败: {ex.Message}".Fail<string>();
        }
    }

    /// <summary>
    /// 设置配置值
    /// </summary>
    public async Task<BaseResponse> SetConfig(string key, string value)
    {
        try
        {
            await configService.SetConfigByKeyAsync(key, value);
            return "配置设置成功".Ok();
        }
        catch (Exception ex)
        {
            return $"设置{key}配置失败: {ex.Message}".Fail();
        }
    }
}