package cmd

import (
	"fmt"
	"io"
	"os"

	"github.com/cloudquery/cloudquery/cli/v6/internal/env"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"

	"github.com/cloudquery/cloudquery/cli/v6/internal/enum"
	"github.com/cloudquery/cloudquery/cli/v6/internal/secrets"
)

func initLogging(noLogFile bool, logLevel *enum.Enum, logFormat *enum.Enum, logConsole bool, logFileName string, logFileOverwrite bool) (*os.File, error) {
	var logFile *os.File
	zerologLevel, err := zerolog.ParseLevel(logLevel.String())
	if err != nil {
		return nil, err
	}
	var writers []io.Writer
	if !noLogFile {
		flags := os.O_CREATE | os.O_WRONLY
		if logFileOverwrite {
			flags |= os.O_TRUNC
		} else {
			flags |= os.O_APPEND
		}
		logFile, err = os.OpenFile(logFileName, flags, 0666)
		if err != nil {
			return nil, err
		}
		if logFormat.String() == "text" {
			// for file logging we don't need color. we can add it as an option but don't think it is useful
			writers = append(writers, zerolog.ConsoleWriter{
				Out:             logFile,
				NoColor:         true,
				FormatTimestamp: formatTimestampUtcRfc3339,
			})
		} else {
			writers = append(writers, logFile)
		}
	}
	if logConsole {
		if err := os.Stdout.Close(); err != nil {
			return nil, fmt.Errorf("failed to close stdout: %w", err)
		}
		if logFormat.String() == "text" {
			writers = append(writers, zerolog.ConsoleWriter{
				Out:             os.Stderr,
				NoColor:         true,
				FormatTimestamp: formatTimestampUtcRfc3339,
			})
		} else {
			writers = append(writers, os.Stderr)
		}
	}
	mw := io.MultiWriter(writers...)
	secretAwareWriter := secrets.NewSecretAwareWriter(mw, secretAwareRedactor)
	log.Logger = zerolog.New(secretAwareWriter).Level(zerologLevel).With().Str("module", "cli").Str("invocation_id", invocationUUID.String()).Timestamp().Logger()
	if env.TenantID() != "" {
		log.Logger = log.Logger.With().Str("tenant_id", env.TenantID()).Logger()
	}
	return logFile, nil
}
