/*
 * Copyright 2021-2024 JetBrains s.r.o.
 *
 * Licensed 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
 *
 * https://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 corescan

import (
	"path/filepath"
	"strings"

	"github.com/JetBrains/qodana-cli/internal/core/startup"
	platformcmd "github.com/JetBrains/qodana-cli/internal/platform/cmd"
	"github.com/JetBrains/qodana-cli/internal/platform/commoncontext"
	"github.com/JetBrains/qodana-cli/internal/platform/qdcontainer"
	"github.com/JetBrains/qodana-cli/internal/platform/qdenv"
)

func CreateContext(
	cliOptions platformcmd.CliOptions,
	commonCtx commoncontext.Context,
	preparedHost startup.PreparedHost,
	qodanaYamlConfig QodanaYamlConfig,
	effectiveConfigurationDir string,
) Context {
	coverageDir := cliOptions.CoverageDir
	if coverageDir == "" {
		if qdenv.IsContainer() {
			coverageDir = qdcontainer.DataCoverageDir
		} else {
			coverageDir = filepath.Join(commonCtx.ProjectDir, ".qodana", "code-coverage")
		}
	}

	commit := strings.TrimPrefix(cliOptions.Commit, "CI")

	return ContextBuilder{
		Analyser:                  commonCtx.Analyzer,
		Id:                        commonCtx.Id,
		IdeDir:                    preparedHost.IdeDir,
		EffectiveConfigurationDir: effectiveConfigurationDir,
		Prod:                      preparedHost.Prod,
		QodanaUploadToken:         preparedHost.QodanaUploadToken,
		ProjectDir:                commonCtx.ProjectDir,
		RepositoryRoot:            commonCtx.RepositoryRoot,
		ResultsDir:                commonCtx.ResultsDir,
		ConfigDir:                 commonCtx.ConfDirPath(),
		LogDir:                    commonCtx.LogDir(),
		QodanaSystemDir:           commonCtx.QodanaSystemDir,
		CacheDir:                  commonCtx.CacheDir,
		ReportDir:                 commonCtx.ReportDir,
		CoverageDir:               coverageDir,
		OnlyDirectory:             cliOptions.OnlyDirectory,
		Env:                       cliOptions.Env_,
		DisableSanity:             cliOptions.DisableSanity,
		ProfileName:               cliOptions.ProfileName,
		ProfilePath:               cliOptions.ProfilePath,
		RunPromo:                  cliOptions.RunPromo,
		Baseline:                  cliOptions.Baseline,
		BaselineIncludeAbsent:     cliOptions.BaselineIncludeAbsent,
		SaveReport:                cliOptions.SaveReport,
		ShowReport:                cliOptions.ShowReport,
		Port:                      cliOptions.Port,
		Property:                  cliOptions.Property,
		Script:                    cliOptions.Script,
		FailThreshold:             cliOptions.FailThreshold,
		Commit:                    commit,
		DiffStart:                 cliOptions.DiffStart,
		DiffEnd:                   cliOptions.DiffEnd,
		ForceLocalChangesScript:   cliOptions.ForceLocalChangesScript,
		ReversePrAnalysis:         cliOptions.ReversePrAnalysis,
		AnalysisId:                cliOptions.AnalysisId,
		Volumes:                   cliOptions.Volumes,
		User:                      cliOptions.User,
		PrintProblems:             cliOptions.PrintProblems,
		GenerateCodeClimateReport: cliOptions.GenerateCodeClimateReport,
		SendBitBucketInsights:     cliOptions.SendBitBucketInsights,
		SkipPull:                  cliOptions.SkipPull,
		FullHistory:               cliOptions.FullHistory,
		ApplyFixes:                cliOptions.ApplyFixes,
		Cleanup:                   cliOptions.Cleanup,
		FixesStrategy:             cliOptions.FixesStrategy,
		NoStatistics:              cliOptions.NoStatistics,
		CdnetSolution:             cliOptions.CdnetSolution,
		CdnetProject:              cliOptions.CdnetProject,
		CdnetConfiguration:        cliOptions.CdnetConfiguration,
		CdnetPlatform:             cliOptions.CdnetPlatform,
		CdnetNoBuild:              cliOptions.CdnetNoBuild,
		ClangCompileCommands:      cliOptions.ClangCompileCommands,
		ClangArgs:                 cliOptions.ClangArgs,
		AnalysisTimeoutMs:         cliOptions.AnalysisTimeoutMs,
		AnalysisTimeoutExitCode:   cliOptions.AnalysisTimeoutExitCode,
		JvmDebugPort:              cliOptions.JvmDebugPort,
		GlobalConfigurationsDir:   cliOptions.GlobalConfigurationsDir,
		GlobalConfigurationId:     cliOptions.GlobalConfigurationId,
		CustomLocalQodanaYamlPath: cliOptions.ConfigName,
		QodanaYamlConfig:          qodanaYamlConfig,
	}.Build()
}
