package common

import (
	"io"
	"os"
	"path/filepath"
)

func AppendFile(source string, destination string) error {
	err := createDirectoryIfNotPresent(filepath.Dir(destination))
	if err != nil {
		return err
	}
	in, err := os.Open(source)
	if err != nil {
		return err
	}
	defer in.Close()

	out, err := os.OpenFile(destination, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		return err
	}
	defer out.Close()

	data, err := io.ReadAll(in)
	if err != nil {
		return err
	}

	_, err = out.Write(data)
	if err != nil {
		return err
	}

	return nil
}

func CopyFile(source string, destination string) error {
	err := createDirectoryIfNotPresent(filepath.Dir(destination))
	if err != nil {
		return err
	}
	in, err := os.Open(source)
	if err != nil {
		return err
	}
	defer in.Close()

	out, err := os.Create(destination)
	if err != nil {
		return err
	}
	defer out.Close()

	data, err := io.ReadAll(in)
	if err != nil {
		return err
	}

	_, err = out.Write(data)
	if err != nil {
		return err
	}

	return nil
}

func MoveFile(source string, destination string) error {
	err := createDirectoryIfNotPresent(filepath.Dir(destination))
	if err != nil {
		return err
	}
	return os.Rename(source, destination)
}

func createDirectoryIfNotPresent(path string) error {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		err := os.MkdirAll(path, 0755)
		if err != nil {
			return err
		}
	}
	return nil
}
