package main

import (
	"crypto/md5"
	"fmt"

	"github.com/gologme/log"
	"github.com/peterbourgon/diskv/v3"
)

var Userdb *diskv.Diskv

func flatTransform(s string) []string {

	t := fmt.Sprintf("%x", md5.Sum([]byte(s)))
	return []string{t}

}

func UserDbInit() {
	// Simplest transform function: put all the data files into the base dir.
	// flatTransform := func(s string) []string { return []string{} }

	// Initialize a new diskv store, rooted at "my-data-dir", with a 1MB cache.
	Userdb = diskv.New(diskv.Options{
		BasePath:     "userdb",
		Transform:    flatTransform,
		CacheSizeMax: 1024 * 1024,
	})

	log.Info("Users Database Created")

	if err := Userdb.WriteString("Uriel", "futura password criptata"); err == nil {
		log.Info("User UserTest Created")
	} else {
		log.Info("Error while creating user", err.Error())
	}

	if err := Userdb.WriteString("Loweel", "futura password criptata"); err == nil {
		log.Info("User LoweelTest Created")
	} else {
		log.Info("Error while creating user", err.Error())
	}

	log.Info("Users Created")

}
