using IWS5.Autotest.IWS5.Api.ApiEndpointFormats;
using IWS5.Autotest.IWS5.Api.Common;
using IWS5.Autotest.IWS5.Api.Common.Pashn.Autotests.Pashn.Api.Common;
using IWS5.Core.DTO;
using IWS5.Core.Models;
using IWS5.Core.Models.Request;
using RestSharp;

namespace IWS5.Autotest.IWS5.Api.Api
{
    public class DocumentControllerApi : BaseAppApi
    {
        private readonly SessionStorage _sessionStorage;

        public DocumentControllerApi(SessionStorage sessionStorage)
            : base(ApiEndpointFormat.Document.Base, sessionStorage)
        {
            _sessionStorage = sessionStorage;
        }

        public async Task<RestResponse<Response<List<DocumentListItemDTO>>>> ListDocumentFields(int workItemReferenceId, bool includingDocumentContent)
        {
            var request = CreateGetRequest(ApiEndpointFormat.Document.List);
            request.AddQueryParameter(nameof(workItemReferenceId), workItemReferenceId.ToString());
            request.AddQueryParameter(nameof(includingDocumentContent), includingDocumentContent.ToString().ToLower());
            return await ExecuteRequest<Response<List<DocumentListItemDTO>>>(request);
        }

        public async Task<RestResponse<DocumentDTO>> GetDocument(int documentId)
        {
            var request = CreateGetRequest($"{documentId}");
            return await ExecuteRequest<DocumentDTO>(request);
        }

        public async Task<RestResponse> UpdateDocumentField(UpdateDocumentFieldListRequest requestPayload)
        {
            var request = CreatePutRequest(ApiEndpointFormat.Document.UpdateFields);
            request.AddJsonBody(requestPayload);
            return await ExecuteRequest<RestResponse>(request);
        }

        public async Task<RestResponse<CodeResponse<DocumentListItemDTO>>> UpdateDocumentContent(UpdateDocumentContentRequest requestPayload)
        {
            var request = CreatePutRequest(ApiEndpointFormat.Document.UpdateContent, requestPayload);
            return await ExecuteRequest<CodeResponse<DocumentListItemDTO>>(request);
        }

        public async Task<RestResponse<CodeResponse<DocumentListItemDTO>>> UpdateDocumentName(RenameDocumentRequest renameDocumentRequest)
        {
            var request = CreatePatchRequest(ApiEndpointFormat.Document.UpdateRename, renameDocumentRequest);
            return await ExecuteRequest<CodeResponse<DocumentListItemDTO>>(request);
        }

        public async Task<RestResponse<CodeResponse<String>>> DeleteDocument(int documentId, bool permanently)
        {
            var request = CreateDeleteRequest(ApiEndpointFormat.Document.Delete);
            request.AddQueryParameter(nameof(documentId), documentId.ToString());
            request.AddQueryParameter(nameof(permanently), permanently.ToString().ToLower());
            return await ExecuteRequest<CodeResponse<String>>(request);
        }

        public async Task<RestResponse<CodeResponse<DocumentListItemDTO>>> AddDocument(AddDocumentRequest documentRequest)
        {            
            var request = CreatePostRequest(ApiEndpointFormat.Document.Add, documentRequest);
            return await ExecuteRequest<CodeResponse<DocumentListItemDTO>>(request);
        }
    }
}
