package models

import (
	"bitbucket.org/robert2206/automation/modules/settings"
	"github.com/jinzhu/gorm"
	// sqlite3 is blank import for lazy load driver and testing database
	_ "github.com/mattn/go-sqlite3"
)

var (
	g      *gorm.DB
	models []interface{}
)

func init() {
	models = append(models,
		&User{},
		&Request{},
		&Task{},
		&Issue{},
	)
}

// Init is a models initializers
func Init() {
	db, err := gorm.Open("sqlite3", "data/test.db")

	if err != nil {
		panic("Failed to connect database")
	}

	defer db.Close()

	// check if logging
	db.LogMode(settings.Ctx.DatabaseLogger)

	// check if migrating
	if settings.Ctx.DatabaseAutomigrate {
		db.Debug().AutoMigrate(models...)
	}
}
