package commands

import (
	"os"
	"os/exec"
	"path/filepath"

	"github.com/contiamo/dev/cli/config"
	"github.com/contiamo/dev/cli/tar"
	"github.com/spf13/cobra"
)

// dumps_dir="/tmp/dumps"
// tmp_filename="/tmp/dump.tar.xz"

// if [ -z "$filename" ]; then
//     echo "Set the filename of the snapshot as the only argument" && exit 1
// fi

// docker-compose down
// docker-compose up -d metadb

// docker exec dev_metadb_1 bash -c '\
// pg_dump hub -h localhost -U user -O --inserts --column-inserts >  hub.sql; \
// pg_dump datastore -h localhost -U user -O --inserts --column-inserts >  datastore.sql; \
// pg_dump simpleidp -h localhost -U user -O --inserts --column-inserts >  idp.sql; \
// pg_dump pantheon -h localhost -U user -O --inserts --column-inserts >  pantheon.sql; \
// '

// mkdir -p $dumps_dir
// rm -rf "$dumps_dir/*"

// docker cp dev_metadb_1:/hub.sql $dumps_dir
// docker cp dev_metadb_1:/datastore.sql $dumps_dir
// docker cp dev_metadb_1:/idp.sql $dumps_dir
// docker cp dev_metadb_1:/pantheon.sql $dumps_dir

// tar cfvJ $filename -C $dumps_dir .
// rm -rf $dumps_dir

const dumpScript = `
pg_dump hub -h localhost -U user -O --inserts --column-inserts >  hub.sql; \
pg_dump datastore -h localhost -U user -O --inserts --column-inserts >  datastore.sql; \
pg_dump simpleidp -h localhost -U user -O --inserts --column-inserts >  idp.sql; \
pg_dump pantheon -h localhost -U user -O --inserts --column-inserts >  pantheon.sql; \
`

func init() {
	snapshotCmd.Flags().StringP("output", "o", "localdev.snapshot", "set the output destination in the localdev env.")
}

var snapshotCmd = &cobra.Command{
	Use:   "snapshot",
	Short: "create a new localdev snapshot",
	Example: `
	localdev snapshot`,
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		output, err := cmd.Flags().GetString("output")
		if err != nil {
			return err
		}

		err = stopLocaldev(cmd, false)
		if err != nil {
			return err
		}

		err = startDB(cmd)
		if err != nil {
			return err
		}

		snapshotter := exec.CommandContext(
			cmd.Context(),
			"docker",
			"exec",
			"dev_metadb_1",
			"bash", "-c",
			dumpScript,
		)
		snapshotter.Dir = config.LocaldevRoot()
		snapshotter.Stderr = cmd.OutOrStderr()
		snapshotter.Stdout = cmd.OutOrStdout()
		snapshotter.Env = os.Environ()

		err = snapshotter.Run()
		if err != nil {
			return err
		}

		tmpDir, err := os.MkdirTemp("", "localdev-snapshot")
		if err != nil {
			return err
		}
		defer os.RemoveAll(tmpDir)

		dumps := []string{"hub.sql", "datastore.sql", "pantheon.sql", "idp.sql"}
		for _, dbDump := range dumps {
			clean := exec.CommandContext(
				cmd.Context(),
				"docker",
				"cp",
				"dev_metadb_1:/"+dbDump,
				tmpDir,
			)
			clean.Dir = config.LocaldevRoot()
			clean.Stderr = cmd.OutOrStderr()
			clean.Stdout = cmd.OutOrStdout()
			clean.Env = os.Environ()

			err = clean.Run()
			if err != nil {
				return err
			}
		}

		dst := filepath.Join(config.LocaldevRoot(), output)

		snapshot, err := os.Create(dst)
		if err != nil {
			return err
		}

		return tar.Compress(tmpDir, snapshot)
	},
}
