package db

import (
	"fmt"
	"os"

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

var Gorm *gorm.DB

func init() {
	var err error
	var dsn string
	connectionName := os.Getenv("CLOUDSQL_CONNECTION_NAME")
	if connectionName != "" {
		dbName := os.Getenv("DB_NAME")
		dbUser := os.Getenv("DB_USER")
		dbPwd := os.Getenv("DB_PASS")
		dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", dbUser, dbPwd, connectionName, dbName)
	} else {
		// dsn := "root:123456@tcp(localhost:3306)/rental?charset=utf8"
		dsn = "homapp:PaqvTcSXUzNc9M9@tcp(34.105.87.213:3306)/homapp?charset=utf8"
	}
	Gorm, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
}
