package io.qameta.allure.restassured;

import io.qameta.allure.attachment.DefaultAttachmentProcessor;
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
import io.qameta.allure.attachment.http.HttpRequestAttachment;
import io.restassured.filter.FilterContext;
import io.restassured.filter.OrderedFilter;
import io.restassured.internal.NameAndValue;
import io.restassured.internal.support.Prettifier;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;

import java.util.HashMap;
import java.util.Map;

import static io.qameta.allure.attachment.http.HttpRequestAttachment.Builder.create;

/**
 * Allure logger filter for Rest-assured.
 */
public class AllureRestAssured implements OrderedFilter {

    @Override
    public Response filter(final FilterableRequestSpecification requestSpec,
                           final FilterableResponseSpecification responseSpec,
                           final FilterContext filterContext) {
        final Prettifier prettifier = new Prettifier();

        final HttpRequestAttachment requestAttachment = create("Request", requestSpec.getURI())
                .withBody(prettifier.getPrettifiedBodyIfPossible(requestSpec))
                .withHeaders(toMapConverter(requestSpec.getHeaders()))
                .withCookies(toMapConverter(requestSpec.getCookies()))
                .build();

        new DefaultAttachmentProcessor().addAttachment(
                requestAttachment,
                new FreemarkerAttachmentRenderer("http-request.ftl")
        );

        return filterContext.next(requestSpec, responseSpec);
    }

    private static Map<String, String> toMapConverter(final Iterable<? extends NameAndValue> items) {
        final Map<String, String> result = new HashMap<>();
        items.forEach(h -> result.put(h.getName(), h.getValue()));
        return result;
    }

    @Override
    public int getOrder() {
        return Integer.MAX_VALUE;
    }
}

@@ -223,16 +223,80 @@
 chment;%0A
+import io.qameta.allure.attachment.http.HttpResponseAttachment;%0A
 import i
@@ -331,16 +331,16 @@
 ontext;%0A
-
 import i
@@ -783,16 +783,102 @@
 .create;
+%0Aimport static io.qameta.allure.attachment.http.HttpResponseAttachment.Builder.create;
 %0A%0A/**%0A *
@@ -1795,22 +1795,41 @@
         
-return
+final Response response =
  filterC
@@ -1868,16 +1868,557 @@
 eSpec);%0A
+        final HttpResponseAttachment responseAttachment = create(response.getStatusLine())%0A                .withResponseCode(response.getStatusCode())%0A                .withHeaders(toMapConverter(response.getHeaders()))%0A                .withBody(prettifier.getPrettifiedBodyIfPossible(response, response.getBody()))%0A                .build();%0A%0A        new DefaultAttachmentProcessor().addAttachment(%0A                responseAttachment,%0A                new FreemarkerAttachmentRenderer(%22http-response.ftl%22)%0A        );%0A%0A        return response;%0A
     %7D%0A%0A 

add rest-assured response (via #71)