package utils

import (
	"canguidev/shelfy/internal/models"
	"fmt"
	"os"
	"gorm.io/gorm"
	"canguidev/shelfy/query"

)

func CreateDefaultFolder(db *gorm.DB) {
	folders := []string{"Film", "Série", "Manga","Magazine"}

	for _, name := range folders {
		path := "upload/" + name

		if _, err := os.Stat(path); os.IsNotExist(err) {
			err := os.MkdirAll(path, 0755) // MkdirAll au cas où des dossiers parents manquent
			if err != nil {
				fmt.Printf("Erreur lors de la création du dossier %s : %v\n", path, err)
			} else {
				pathDownload := models.PathDownload{
					Path:     path,
					PathName: name,
				}
				q := query.Use(db)
				if err := q.PathDownload.Create(&pathDownload); err != nil {
					fmt.Printf(`{"error": "Failed to create path %%s\n"}`, err)
					return
				}
				fmt.Printf("Dossier créé : %s\n", path)
			}
		} else {
			fmt.Printf("Dossier déjà existant : %s\n", path)
		}
	}
}