package com.test.musichouduan.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * Knife4j配置
 */
@Configuration
public class SwaggerConfig {

    @Bean
    public OpenAPI openAPI() {
        // 添加服务器信息
        List<Server> servers = new ArrayList<>();
        servers.add(new Server().url("/").description("本地服务"));

        return new OpenAPI()
                .info(new Info()
                        .title("音乐应用API文档")
                        .description("音乐应用后端API接口文档")
                        .version("v1.0.0")
                        .contact(new Contact()
                                .name("Admin")
                                .email("admin@example.com")
                                .url("https://example.com"))
                        .license(new License()
                                .name("Apache 2.0")
                                .url("https://www.apache.org/licenses/LICENSE-2.0.html")))
                .externalDocs(new ExternalDocumentation()
                        .description("项目文档")
                        .url("https://example.com/docs"))
                .servers(servers)
                .addSecurityItem(new SecurityRequirement().addList("bearerAuth"))
                .components(new Components()
                        .addSecuritySchemes("bearerAuth", new SecurityScheme()
                                .name("bearerAuth")
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer")
                                .bearerFormat("JWT")));
    }

    @Bean
    public GroupedOpenApi userApi() {
        return GroupedOpenApi.builder()
                .group("用户接口")
                .pathsToMatch("/api/user/**")
                .build();
    }

    @Bean
    public GroupedOpenApi musicApi() {
        return GroupedOpenApi.builder()
                .group("音乐接口")
                .pathsToMatch("/api/music/**")
                .build();
    }

    @Bean
    public GroupedOpenApi singerApi() {
        return GroupedOpenApi.builder()
                .group("歌手接口")
                .pathsToMatch("/api/singer/**")
                .build();
    }

    @Bean
    public GroupedOpenApi playlistApi() {
        return GroupedOpenApi.builder()
                .group("歌单接口")
                .pathsToMatch("/api/playlist/**")
                .build();
    }

    @Bean
    public GroupedOpenApi commentApi() {
        return GroupedOpenApi.builder()
                .group("评论接口")
                .pathsToMatch("/api/comment/**")
                .build();
    }

    @Bean
    public GroupedOpenApi typeApi() {
        return GroupedOpenApi.builder()
                .group("类型接口")
                .pathsToMatch("/api/type/**")
                .build();
    }

    @Bean
    public GroupedOpenApi bannerApi() {
        return GroupedOpenApi.builder()
                .group("轮播图接口")
                .pathsToMatch("/api/banner/**")
                .build();
    }

    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("管理员接口")
                .pathsToMatch("/api/admin/**")
                .build();
    }
}
