package main

import (
	"encoding/json"
	"log/slog"
	"os"

	"github.com/urfave/cli/v2"

	"git.sr.ht/~ashkeel/strimertul/database"
	"git.sr.ht/~ashkeel/strimertul/utils"
)

func cliImport(ctx *cli.Context) error {
	inStream := os.Stdin
	fileArg := ctx.String("file")
	if fileArg != "" {
		file, err := os.Open(fileArg)
		if err != nil {
			return fatalError(err, "could not open import file for reading")
		}
		defer utils.Close(file)
		inStream = file
	}
	var entries map[string]string
	err := json.NewDecoder(inStream).Decode(&entries)
	if err != nil {
		return fatalError(err, "could not decode import file")
	}

	driver, err := database.GetDatabaseDriver(ctx)
	if err != nil {
		return fatalError(err, "could not open database")
	}

	err = driver.Import(entries)
	if err != nil {
		return fatalError(err, "import failed")
	}

	slog.Info("Imported database from file")
	return nil
}

func cliRestore(ctx *cli.Context) error {
	inStream := os.Stdin
	fileArg := ctx.String("file")
	if fileArg != "" {
		file, err := os.Open(fileArg)
		if err != nil {
			return fatalError(err, "could not open import file for reading")
		}
		defer utils.Close(file)
		inStream = file
	}

	driver, err := database.GetDatabaseDriver(ctx)
	if err != nil {
		return fatalError(err, "could not open database")
	}

	err = driver.Restore(inStream)
	if err != nil {
		return fatalError(err, "restore failed")
	}

	slog.Info("Restored database from backup")
	return nil
}

func cliExport(ctx *cli.Context) error {
	outStream := os.Stdout
	fileArg := ctx.String("file")
	if fileArg != "" {
		file, err := os.Create(fileArg)
		if err != nil {
			return fatalError(err, "could not open output file for writing")
		}
		defer utils.Close(file)
		outStream = file
	}

	driver, err := database.GetDatabaseDriver(ctx)
	if err != nil {
		return fatalError(err, "could not open database")
	}

	err = driver.Export(outStream)
	if err != nil {
		return fatalError(err, "export failed")
	}

	slog.Info("Exported database")
	return nil
}
