/**
# Copyright (c) Advanced Micro Devices, Inc. All rights reserved.
#
# 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
#
#     http://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 logger

import (
	"io"
	"log/slog"
	"os"
	"os/user"
	"path/filepath"
	"sync"
)

var (
	logdir    = "/var/log/"
	logfile   = "amd-container-runtime.log"
	logPrefix = "amd-container-runtime "
	once      sync.Once
)

// SetLogPrefix sets prefix in the log to be exporter or testrunner
func SetLogPrefix(prefix string) {
	logPrefix = prefix
}

// SetLogFile sets the log file name
func SetLogFile(file string) {
	logfile = file
}

// SetLogDir sets the path to the directory of logs
func SetLogDir() {

	isWriteable := func(path string) bool {
		// Create a temporary file in the specified directory.
		// os.CreateTemp will return an error if the directory is not writable.
		file, err := os.CreateTemp(path, "tmp-test-")
		if err != nil {
			return false
		}
		file.Close()           // Close the file
		os.Remove(file.Name()) // Clean up the temporary file

		return true
	}

	if os.Getenv("LOGDIR") != "" {
		logdir = os.Getenv("LOGDIR")

		//check if the user has permission to write to this location
		if !isWriteable(logdir) {
			slog.Error("User doesn't have write permission for the specified directory", "dir", logdir)
			os.Exit(1)
		}

		return
	}

	// Get the current user's information.
	currentUser, err := user.Current()
	if err != nil {
		slog.Error("Failed to get current user", "error", err)
		os.Exit(1)
	}
	// for root user, log dir is /var/log
	if currentUser.Uid != "0" {
		//Non-Root user, setting log directory to user's home directory
		homeDir, err := os.UserHomeDir()
		if err != nil {
			slog.Error("Failed to get user home directory", "error", err)
			os.Exit(1)
		}
		logdir = homeDir
	}
}

func initLogger(console bool) {
	var out io.Writer
	if console {
		out = os.Stdout
	} else {
		SetLogDir()

		outfile, err := os.OpenFile(filepath.Join(logdir, logfile),
			os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
		if err != nil {
			return
		}
		out = outfile
	}

	opts := &slog.HandlerOptions{
		Level: slog.LevelDebug,
		ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
			if a.Key == slog.MessageKey {
				a.Value = slog.StringValue(logPrefix + a.Value.String())
			}
			return a
		},
	}
	slog.SetDefault(slog.New(slog.NewTextHandler(out, opts)))
}

func Init(console bool) {
	init := func() {
		initLogger(console)
	}
	once.Do(init)
}
