package templategen

import (
	"github.com/Pena-Co-Ltd/amocrm_templategen_back/dal/model"
	"github.com/Pena-Co-Ltd/amocrm_templategen_back/yadisk"
)

func YaDiskGenerateDoc(template *model.Template, saveFolder string, client *YaDisk.Client, data interface{}) error {
	filename := GenerateDocName(template.Name, template.UserID)

	// Download file
	downloaded := TempDownloaded + "/" + filename
	err := client.DownloadResource(template.Filename, downloaded)
	if err != nil {
		return err
	}

	// Generate file
	generated := TempGenerated + "/" + filename
	err = GenerateFile(filename, data)
	if err != nil {
		return err
	}

	// Upload file
	err = client.ResourcesUpload(saveFolder, generated)
	if err != nil {
		return err
	}

	// Delete temps
	err = DeleteDocument(downloaded)
	if err != nil {
		return err
	}

	err = DeleteDocument(generated)

	return err
}
