package enmasse.controller.api.osb.v2.bind;

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import enmasse.controller.address.AddressManager;
import enmasse.controller.api.osb.v2.EmptyResponse;
import enmasse.controller.api.osb.v2.OSBServiceBase;
import enmasse.controller.flavor.FlavorRepository;
import enmasse.controller.instance.InstanceManager;
import enmasse.controller.model.Instance;

@Path("/v2/service_instances/{instanceId}/service_bindings/{bindingId}")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class OSBBindingService extends OSBServiceBase {

    public OSBBindingService(InstanceManager instanceManager, AddressManager addressManager, FlavorRepository flavorRepository) {
        super(instanceManager, addressManager, flavorRepository);
    }

    @PUT
    public Response bindServiceInstance(@PathParam("instanceId") String instanceId, @PathParam("bindingId") String bindingId, BindRequest bindRequest) {
        log.info("Received bind request for instance {}, binding {} (service id {}, plan id {})",
                instanceId, bindingId, bindRequest.getServiceId(), bindRequest.getPlanId());
        Instance instance = findInstanceByDestinationUuid(instanceId)
                .orElseThrow(() -> new NotFoundException("Service instance " + instanceId + " does not exist"));

        Map<String, String> credentials = new HashMap<>();
        credentials.put("namespace", instance.id().getNamespace());
        instance.messagingHost().ifPresent(s -> credentials.put("messagingHost", s));
        instance.mqttHost().ifPresent(s -> credentials.put("mqttHost", s));
        instance.consoleHost().ifPresent(s -> credentials.put("consoleHost", s));

        BindResponse response = new BindResponse(credentials);
        return Response.status(Response.Status.CREATED).entity(response).build();        // TODO: return 200 OK, when binding already exists
    }

    @DELETE
    public Response unbindServiceInstance(@PathParam("instanceId") String instanceId, @PathParam("bindingId") String bindingId) {
        log.info("Received unbind request for instance {}, binding {}",
                instanceId, bindingId);
        Instance instance = findInstanceByDestinationUuid(instanceId)
                .orElseThrow(() -> new NotFoundException("Service instance " + instanceId + " does not exist"));

        return Response.ok(new EmptyResponse()).build();
    }

}

@@ -38,16 +38,362 @@
 .bind;%0A%0A
+import enmasse.controller.address.AddressManager;%0Aimport enmasse.controller.api.osb.v2.EmptyResponse;%0Aimport enmasse.controller.api.osb.v2.OSBServiceBase;%0Aimport enmasse.controller.flavor.FlavorRepository;%0Aimport enmasse.controller.instance.InstanceManager;%0Aimport enmasse.controller.model.Destination;%0Aimport enmasse.controller.model.Instance;%0A%0A
 import j
@@ -708,309 +708,8 @@
 e;%0A%0A
-import enmasse.controller.address.AddressManager;%0Aimport enmasse.controller.api.osb.v2.EmptyResponse;%0Aimport enmasse.controller.api.osb.v2.OSBServiceBase;%0Aimport enmasse.controller.flavor.FlavorRepository;%0Aimport enmasse.controller.instance.InstanceManager;%0Aimport enmasse.controller.model.Instance;%0A%0A
 @Pat
@@ -1646,32 +1646,302 @@
  not exist%22));%0A%0A
+        Destination destination = findDestination(instance, instanceId)  // TODO: replace this and findInstanceByDestinationUuid so it returns both objects%0A                .orElseThrow(() -%3E new NotFoundException(%22Service instance %22 + instanceId + %22 does not exist%22));%0A%0A
         Map%3CStri
@@ -2294,16 +2294,317 @@
 t%22, s));
+%0A        credentials.put(%22destination-address%22, destination.address());%0A        credentials.put(%22internal-messaging-host%22, %22messaging.%22 + instance.id().getNamespace() + %22.svc.cluster.local%22);%0A        credentials.put(%22internal-mqtt-host%22, %22mqtt.%22 + instance.id().getNamespace() + %22.svc.cluster.local%22);
 %0A%0A      

Add internal-messaging-host and internal-mqtt-host in the credentials passed back after an OSB bind request