// seed_users.go
package main

import (
	"cangui/whatsapp/backend/models"
	"fmt"
	"golang.org/x/crypto/bcrypt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"os"
)

func main() {
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true",
		os.Getenv("DB_USER"),
		os.Getenv("DB_PASS"),
		os.Getenv("DB_HOST"),
		os.Getenv("DB_PORT"),
		os.Getenv("DB_NAME"),
	)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	users := []models.User{
		{
			Email:    "info@pointvirgule.net",
			Password: hash("test"),
			SSOID:    "admin001",
			Role:     models.ROLE_ADMIN,
			WhatsappToken: "EAAYuBH5IDeQBOZBT3lVkpW48657qUFarq4ihCR9aUzlatERQsPxkKuK3bFFHnTbyYpfcPmIdZCko4FJupceC52dunyIuIidTZBZCFTbTaVkQfYZBfJu3rtr6B3ZBtwcVFnm0AVXi8Lj5TmZCPLcEEgxupVtlVdsOvIdMpOWJZCOBC56FzTgA8fRijzFNMlOLuPAEW7Y1f2eMVd5Ku3avy6m5d4JAxrxZCcv1fZBfLXWLZBV",
			WhatsappPhoneNumberID: "644541092077960",
		},
		{
			Email:    "canguijc@gmail.com",
			Password: hash("test"),
			SSOID:    "client001",
			Role:     models.ROLE_CLIENT,
			WhatsappToken: "EAAYuBH5IDeQBOZBT3lVkpW48657qUFarq4ihCR9aUzlatERQsPxkKuK3bFFHnTbyYpfcPmIdZCko4FJupceC52dunyIuIidTZBZCFTbTaVkQfYZBfJu3rtr6B3ZBtwcVFnm0AVXi8Lj5TmZCPLcEEgxupVtlVdsOvIdMpOWJZCOBC56FzTgA8fRijzFNMlOLuPAEW7Y1f2eMVd5Ku3avy6m5d4JAxrxZCcv1fZBfLXWLZBV",
			WhatsappPhoneNumberID: "644541092077960",
		},
	}

	for _, user := range users {
		var existing models.User
		db.Where("email = ?", user.Email).First(&existing)
		if existing.ID == 0 {
			db.Create(&user)
			fmt.Println("✅ Créé:", user.Email)
		} else {
			fmt.Println("⏩ Existe déjà:", user.Email)
		}
	}
}

func hash(password string) string {
	h, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	return string(h)
}
