package com.interplug.qcast.config.swagger;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
@RequiredArgsConstructor
@Slf4j
public class SwaggerConfig {

  @Autowired Environment env;

  @Bean
  public OpenAPI baseApi() {
    String activeProfile = env.getProperty("spring.profiles.active");
    if (log.isDebugEnabled()) {
      log.debug("Active profile: {}", activeProfile);
    }
    OpenAPI openAPI = new OpenAPI();
    openAPI.info(
        new Info().title("QCast API").version("1.0").description("QCast API Documentation"));
    return openAPI;
  }

  @Bean
  public GroupedOpenApi allApi() {
    return GroupedOpenApi.builder().group("ALL").packagesToScan("com.interplug.qcast.biz").addOpenApiCustomizer(openApiCustomizer()).build();
  }
  
  @Bean
  public OpenApiCustomizer openApiCustomizer() {
    Parameter lang = new Parameter().name("lang").description("Language").in("header").schema(new StringSchema());
    return openApi -> openApi.getPaths().values().forEach(
        pathItem -> pathItem.readOperations().forEach(
            operation -> operation.addParametersItem(lang)
        )
    );
  }
}
