/*
 * Copyright 2021 Red Hat
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.apicurio.registry.examples;

import java.io.IOException;
import java.net.URI;
import java.time.Instant;
import java.util.UUID;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

import org.jboss.logging.Logger;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.apicurio.registry.utils.cloudevents.CloudEvent;
import io.apicurio.registry.utils.cloudevents.CloudEventsWsProvider;
import io.cloudevents.core.builder.CloudEventBuilder;

/**
 * @author Fabian Martinez
 */
@ApplicationScoped
@Path("/")
public class OrdersResource {

    private static final Logger log = Logger.getLogger(OrdersResource.class);

    @POST
    @Path("order")
    public CloudEvent<ProcessedOrder> newOrder(CloudEvent<NewOrder> event) throws JsonParseException, JsonMappingException, IOException {
        log.info("Received cloud event of type "+event.type());

        NewOrder neworder = event.data();

        ProcessedOrder order = new ProcessedOrder();
        order.setOrderId(UUID.randomUUID().toString());
        log.info("Processing order "+order.getOrderId());
        order.setItemId(neworder.getItemId());
        order.setQuantity(neworder.getQuantity());
        order.setProcessingTimestamp(Instant.now().toString());
        order.setProcessedBy("orders-service");
        order.setApproved(true);

        return CloudEvent.from(
                    CloudEventBuilder.v1()
                            .withId(UUID.randomUUID().toString())
                            .withSource(URI.create("apicurio-registry-example-api"))
                            .withType(ProcessedOrder.processedOrderEventType)
                            .build(),

                    "/apicurio/"+ProcessedOrder.processedOrderEventType+"/1",

                    order);
    }

    @GET
    @Path("/purchase/{item-id}/{quantity}")
    public Response start(@PathParam("item-id") String itemId, @PathParam("quantity") Integer quantity) {

        NewOrder order = new NewOrder();
        order.setItemId(itemId);
        order.setQuantity(quantity);

        CloudEvent<NewOrder> ce = CloudEvent.from(
                CloudEventBuilder.v1()
                        .withId(UUID.randomUUID().toString())
                        .withSource(URI.create("apicurio-registry-example-client-test"))
                        .withType(NewOrder.newOrderEventType)
                        .build(),

                "/apicurio/"+NewOrder.newOrderEventType+"/1",

                order);

        Response res = ResteasyClientBuilder.newClient().target("http://localhost:8082/order")

                //magic happening here
                .register(CloudEventsWsProvider.class)

                .request()
                .buildPost(Entity.entity(ce, "application/json"))
                .invoke();
        return res;
    }

}