package main

import (
	"fmt"
	"strings"

	"github.com/JetBrains/qodana-cli/internal/platform/thirdpartyscan"
	"github.com/JetBrains/qodana-cli/internal/platform/utils"
	log "github.com/sirupsen/logrus"
)

// Find qodana.yaml, run bootstrap, find enabled checks and return them formatted as an argument for clang-tidy.
func processConfig(c thirdpartyscan.Context) (string, error) {
	var excludeRules []string
	var includeRules []string

	yaml := c.QodanaYamlConfig()
	var checks string
	utils.Bootstrap(yaml.Bootstrap, c.ProjectDir())
	if yaml.Version != "" || len(yaml.Includes) > 0 || len(yaml.Excludes) > 0 {
		fmt.Println("Found qodana.yaml. Note that only bootstrap command and inspection names from include and exclude sections are supported.")
		for _, include := range yaml.Includes {
			if strings.HasPrefix(strings.TrimSpace(include.Name), "clion-") {
				continue
			}
			if strings.ContainsAny(include.Name, "\"") {
				log.Warnf("Skipping include rule with invalid characters: %s", include.Name)
				continue
			}
			includeRules = append(includeRules, include.Name)
		}
		for _, exclude := range yaml.Excludes {
			if strings.ContainsAny(exclude.Name, "\"") {
				log.Warnf("Skipping exclude rule with invalid characters: %s", exclude.Name)
				continue
			}
			excludeRules = append(excludeRules, exclude.Name)
		}
	}
	plusChecks := strings.Join(includeRules, ",")
	for i, minusCheck := range excludeRules {
		excludeRules[i] = "-" + minusCheck
	}
	minusChecks := strings.Join(excludeRules, ",")
	if plusChecks != "" && minusChecks != "" {
		checks = fmt.Sprintf("--checks=%s,%s", plusChecks, minusChecks)
	} else if plusChecks != "" {
		checks = fmt.Sprintf("--checks=%s", plusChecks)
	} else if minusChecks != "" {
		checks = fmt.Sprintf("--checks=*,%s", minusChecks)
	} else {
		// Default to all checks when no configuration is provided
		// This is needed because recent clang-tidy versions enable no checks by default
		checks = "--checks=*"
	}
	return checks, nil
}
