package com.airlock.iam.admin.infrastructure.resource.prot.techclient;

import ch.ergon.restal.jsonapi.document.data.CollectionDocument;
import com.airlock.iam.admin.infrastructure.generated.transfer.response.techclient.PlanUsageDataCollectionDocument;
import com.airlock.iam.admin.infrastructure.transfer.response.techclient.PlanUsageData;
import com.airlock.iam.base.infrastructure.error.ErrorResponse;
import com.airlock.iam.base.infrastructure.mapper.entity.RestMapper;
import com.airlock.iam.techclient.admin.application.service.SubscriptionService;
import com.airlock.iam.techclient.admin.domain.model.Subscription;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;
import java.util.Optional;

@Path("/subscriptions/{subscriptionId}")
public class SubscriptionResource {
   private final RestMapper restMapper;
   private final Provider<SubscriptionService> service;

   @Inject
   public SubscriptionResource(RestMapper restMapper, Provider<SubscriptionService> service) {
      this.restMapper = restMapper;
      this.service = service;
   }

   @ApiResponses(
      {
            @ApiResponse(
               responseCode = "200",
               description = "The response contains all Plan-usages.",
               content = {
                     @Content(mediaType = "application/vnd.api+json", schema = @Schema(implementation = PlanUsageDataCollectionDocument.class)),
                     @Content(mediaType = "application/json", schema = @Schema(implementation = PlanUsageDataCollectionDocument.class))
               }
            ),
            @ApiResponse(
               responseCode = "404",
               description = "The subscription does not exist.",
               content = {
                     @Content(mediaType = "application/vnd.api+json", schema = @Schema(ref = "json-api.yaml#/components/schemas/ErrorDocumentResponse")),
                     @Content(mediaType = "application/json", schema = @Schema(ref = "json-api.yaml#/components/schemas/ErrorDocumentResponse"))
               }
            )
      }
   )
   @GET
   @Operation(
      description = "Retrieves all Plan-usages for the specified subscription.\n<div class=\"iam-resource-return\">The collection of Plan-usages.</div>",
      tags = "Technical Clients",
      summary = "List plan usages"
   )
   @Produces({"application/vnd.api+json", "application/json;qs=0.9"})
   @Path("plan-usages")
   public Response retrievePlanUsages(@Parameter(description = "The identification of the subscription.") @PathParam("subscriptionId") String subscriptionId) {
      Optional<Subscription> subscription = ((SubscriptionService)this.service.get()).findSubscription(Subscription.subscriptionId(subscriptionId));
      if (subscription.isEmpty()) {
         return ErrorResponse.notFound().build().toResponse();
      }

      CollectionDocument<PlanUsageData> document = this.restMapper.forEntities(subscription.get().getPlanUsages(), PlanUsageData.class).toCollection();
      return Response.ok(document).build();
   }
}
