package engine

import (
	"encoding/hex"
	"fmt"
	"os"
	"path/filepath"
	"time"

	"github.com/deranjer/gvc/common/database"
)

// CreateInitialCommit copies the files over and compresses them if they are not in the NoCompress struct
func (m *Manager) CreateInitialCommit(fileList []database.File, commitMessage string) error { // ONLY HAPPENS FOR MASTER I THINK, SO NO BRANCH NEEDED
	//Need to deduplicate so we aren't storing duplicates of files, storing all the files in one folder won't work, will need something like git
	//For initial commit no changes are made to files, so don't store anything, just save the list so you can send to server
	var initialCommit database.Commit
	initialCommit.Branch = "master"
	hashBytes, err := CreateCommitHash(fileList, commitMessage)
	if err != nil {
		return err
	}
	currentTime := time.Now()
	initialCommit.CommitHash = hashBytes
	initialCommit.Number = 1
	initialCommit.TrackedFiles = fileList
	initialCommit.Date = currentTime.String()
	folder := m.FilePaths.ObjectFolder + string(filepath.Separator) + hex.EncodeToString(hashBytes)
	err = os.Mkdir(folder, 0666)
	if err != nil {
		return fmt.Errorf("unable to create commit directory in object dir: %s err: %s", folder, err)
	}
	for _, file := range fileList {
		ConvertFileForStorage(&file, folder)
	}
	//var hashList [][]byte
	return nil

}
