package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/joho/godotenv"
)

func loadEnv() {
	exePath, _ := os.Executable()
	exeDir := filepath.Dir(exePath)

	// Normalize both to lowercase absolute paths for Windows safety
	exePathLower := strings.ToLower(exePath)
	tempDirLower := strings.ToLower(filepath.Clean(os.TempDir()))

	// Heuristic: if exe lives *inside* the system temp dir → assume go run
	if strings.HasPrefix(exePathLower, tempDirLower) {
		fmt.Println("Detected go run – loading ../.env")
		err := godotenv.Load("../.env")
		if err != nil {
			fmt.Println("ERROR loading .env:", err)
		} else {
			fmt.Println(".env successfully loaded")
		}

		return
	}

	// Otherwise → normal compiled exe
	fmt.Println("Detected compiled exe – loading exeDir/.env")
	if err := godotenv.Load(filepath.Join(exeDir, ".env")); err != nil {
		fmt.Println("Didn't find exeDir/.env – trying ../.env as fallback")
		err := godotenv.Load("../.env")
		if err != nil {
			fmt.Println("ERROR loading .env:", err)
		} else {
			fmt.Println(".env successfully loaded")
		}
	}

}
