package system

import (
	"os"

	"numenor-labs.us/dsfx/internal/lib/disk"
)

// Default returns a default implementation of the System interface.
func Default() System {
	return &osSystem{}
}

type osSystem struct{}

// Args returns the command-line arguments passed to the program. The name of the
// program is filtered out, so it only returns the arguments provided by the user.
// It implements the System interface.
func (s *osSystem) Args() []string {
	return os.Args[1:]
}

// Arg returns the command-line argument at the specified index. If the index is
// out of range, it returns an empty string.
// // It implements the System interface.
func (s *osSystem) Arg(i int) string {
	if i < 0 || i >= len(os.Args) {
		return ""
	}
	return os.Args[i+1] // +1 to skip the program name
}

// UserHomeDir returns the user's home directory.
// It implements the System interface.
func (s *osSystem) UserHomeDir() (string, error) {
	return os.UserHomeDir()
}

// UserConfigDir returns the user's configuration directory.
// It implements the System interface.
func (s *osSystem) UserConfigDir() (string, error) {
	return os.UserConfigDir()
}

// UserCacheDir returns the user's cache directory.
// It implements the System interface.
func (s *osSystem) UserCacheDir() (string, error) {
	return os.UserCacheDir()
}

// TempDir returns the directory used for temporary files.
// It implements the System interface.
func (s *osSystem) TempDir() string {
	return os.TempDir()
}

// Stdout returns the standard output file.
// It implements the System interface.
func (s *osSystem) Stdout() disk.File {
	return os.Stdout
}

// Stderr returns the standard error file.
// It implements the System interface.
func (s *osSystem) Stderr() disk.File {
	return os.Stderr
}

// Exit terminates the program with the given exit code.
// It implements the System interface.
func (s *osSystem) Exit(code int) {
	os.Exit(code)
}

// GetEnv retrieves the value of the environment variable named by key.
// It implements the System interface.
func (s *osSystem) GetEnv(key string) string {
	return os.Getenv(key)
}

// SetEnv sets the value of the environment variable named by key to value.
// It implements the System interface.
func (s *osSystem) SetEnv(key, value string) error {
	return os.Setenv(key, value)
}