package store

import (
	"errors"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type FeatureFlag struct {
	ID      uint   `gorm:"primaryKey"`
	Key     string `gorm:"uniqueIndex;size:191"`
	Enabled bool
}

func Open(dialect, dsn string) (*gorm.DB, error) {
	switch dialect {
	case "mysql", "mariadb":
		db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err != nil {
			return nil, err
		}
		if err := db.AutoMigrate(&FeatureFlag{}); err != nil {
			return nil, err
		}
		return db, nil
	default:
		return nil, errors.New("unsupported app db dialect")
	}
}

func OpenPresta(dialect, dsn string) (*gorm.DB, error) {
	switch dialect {
	case "mysql", "mariadb":
		return gorm.Open(mysql.Open(dsn), &gorm.Config{})
	default:
		return nil, errors.New("unsupported prestashop db dialect")
	}
}
