package generator

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

	"github.com/rs/zerolog/log"
)

func writeOrWarn(outFilePath string, data string, force bool) error {
	if !force && fileExists(outFilePath) {
		return fmt.Errorf("cannot write %s, file already exists", outFilePath)
	}
	if !dirExists(filepath.Dir(outFilePath)) {
		log.Debug().Msgf("Creating directory %s", filepath.Dir(outFilePath))
		err := os.MkdirAll(filepath.Dir(outFilePath), 0o755)
		if err != nil {
			return err
		}
	}
	file, err := os.Create(outFilePath)
	if err != nil {
		return err
	}
	data = "# Generated by Gabo (https://github.com/ashishb/gabo)\n" + data
	_, err = io.WriteString(file, data)
	if err != nil {
		return err
	}
	log.Info().Msgf("Wrote file %s", outFilePath)
	return file.Close()
}

func fileExists(path string) bool {
	_, err := os.Stat(path)
	return err == nil
}

func dirExists(path string) bool {
	info, err := os.Stat(path)
	if err != nil {
		return false
	}
	if !info.IsDir() {
		log.Warn().Msgf("Path exists but is not a directory: %s", path)
		return false
	}
	return true
}
