/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 com.michelin.kstreamplify.controller;

import com.michelin.kstreamplify.initializer.KafkaStreamsStarter;
import com.michelin.kstreamplify.service.TopologyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/** Kafka Streams controller for topology. */
@RestController
@ConditionalOnBean(KafkaStreamsStarter.class)
@Tag(name = "Topology", description = "Topology Controller")
public class TopologyController {
    private final TopologyService topologyService;

    /**
     * Constructor.
     *
     * @param topologyService The topology service
     */
    public TopologyController(TopologyService topologyService) {
        this.topologyService = topologyService;
    }

    /**
     * Get the Kafka Streams topology.
     *
     * @return The Kafka Streams topology
     */
    @Operation(summary = "Get the Kafka Streams topology")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
    @GetMapping("/${topology.path:topology}")
    public ResponseEntity<String> topology() {
        return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(topologyService.getTopology());
    }
}
