package models

import (
	"fmt"
	"log"

	"git.trj.tw/golang/mtgbot/modules/config"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
)

var x *sqlx.DB

// NewDB -
func NewDB() *sqlx.DB {
	var err error
	conf := config.GetConf()
	userPassStr := conf.Database.User
	if len(conf.Database.Pass) > 0 {
		userPassStr += ":" + conf.Database.Pass
	}

	connStr := fmt.Sprintf("postgres://%s@%s/%s?sslmode=disable", userPassStr, conf.Database.Host, conf.Database.DB)

	x, err = sqlx.Connect("postgres", connStr)

	if err != nil {
		log.Fatal(err)
	}
	x.SetMaxIdleConns(10)
	x.SetMaxOpenConns(100)
	err = x.Ping()
	if err != nil {
		log.Fatal(err)
	}

	return x
}

// String - get string ptr
func String(s string) *string {
	return &s
}

// Int - get int ptr
func Int(i int) *int {
	return &i
}

// OrderStr -
type OrderStr string

func (o OrderStr) String() string {
	return string(o)
}

// OpStr -
type OpStr string

func (s OpStr) String() string {
	return string(s)
}

// Op var
const (
	SearchOpEq  OpStr = "="
	SearchOpNeq       = "!="
	SearchOpLt        = "<"
	SearchOpLte       = "<="
	SearchOpGt        = ">"
	SearchOpGte       = ">="
)
