package payment

import (
	"fmt"

	"github.com/glebarez/sqlite"
	"gorm.io/gorm"
)

type Data struct{ gormDB *reloadableDB }

type reloadableDB struct{ db *gorm.DB }

func newReloadableDB(db *gorm.DB, _ any) *reloadableDB { return &reloadableDB{db: db} }
func (r *reloadableDB) close() {
	if r == nil || r.db == nil {
		return
	}
	if sqlDB, err := r.db.DB(); err == nil {
		_ = sqlDB.Close()
	}
}
func (d *Data) DB() *gorm.DB { return d.gormDB.db }

func openWithDriver(driver, dsn string) (*gorm.DB, error) {
	if driver != "sqlite" {
		return nil, fmt.Errorf("unsupported test database driver %q", driver)
	}
	return gorm.Open(sqlite.Open(dsn), &gorm.Config{})
}
