using RestSharp;

namespace LazyBear.MCP.Services.GitLab;

/// <summary>
/// Обертка над RestSharp RestClient для GitLab API
/// </summary>
public sealed class GitLabApiClient : IDisposable
{
    public RestClient RestClient { get; }

    /// <summary>
    /// Конструктор
    /// </summary>
    /// <param name="url">URL GitLab</param>
    public GitLabApiClient(string url)
    {
        _restClient = new RestClient(url);
    }

    private readonly RestClient _restClient;

    /// <summary>
    /// Создание запроса GET
    /// </summary>
    public RestRequest GetRequest(string resource)
    {
        var request = new RestRequest(resource, Method.Get);
        return request;
    }

    /// <summary>
    /// Создание запроса POST
    /// </summary>
    public RestRequest PostRequest(string resource)
    {
        var request = new RestRequest(resource, Method.Post);
        return request;
    }

    /// <summary>
    /// Создание запроса PUT
    /// </summary>
    public RestRequest PutRequest(string resource)
    {
        var request = new RestRequest(resource, Method.Put);
        return request;
    }

    /// <summary>
    /// Создание запроса DELETE
    /// </summary>
    public RestRequest DeleteRequest(string resource)
    {
        var request = new RestRequest(resource, Method.Delete);
        return request;
    }

    /// <summary>
    /// Выполнение запроса
    /// </summary>
    public async System.Threading.Tasks.Task<RestResponse> ExecuteAsync(RestRequest request, System.Threading.CancellationToken? cancellationToken = null)
    {
        if (cancellationToken.HasValue)
        {
            return await _restClient.ExecuteAsync(request, cancellationToken.Value);
        }
        return await _restClient.ExecuteAsync(request);
    }

    public void Dispose()
    {
        _restClient.Dispose();
    }
}
