package database

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"os"
	"time"
)

func OpenGormDB(source string) *gorm.DB {
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold:             time.Second, // Slow SQL threshold
			LogLevel:                  logger.Info, // Log level
			IgnoreRecordNotFoundError: true,        // Ignore ErrRecordNotFound error for logger
			Colorful:                  false,       // Disable color
		},
	)
	fmt.Println("starting db...")
	db, err := gorm.Open(mysql.Open(source), &gorm.Config{
		Logger: newLogger,
	})
	if err != nil {
		panic(err)
	}
	return db
}

func OpenGormPGDB(source string, logMode string) *gorm.DB {
	logx.Infof("starting db...")
	db, err := gorm.Open(postgres.New(postgres.Config{
		DSN:                  source,
		PreferSimpleProtocol: true, // disables implicit prepared statement usage
	}), &gorm.Config{
		Logger:               NewLogger(logMode), //newLogger,
		DisableAutomaticPing: true,
	})
	if err != nil {
		panic(err)
	}
	return db
}

func NewLogger(logType string) logger.Interface {
	if logType == "console" {
		return logger.New(
			log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
			logger.Config{
				SlowThreshold:             time.Second, // Slow SQL threshold
				LogLevel:                  logger.Info, // Log level
				IgnoreRecordNotFoundError: true,        // Ignore ErrRecordNotFound error for logger
				Colorful:                  false,       // Disable color
			},
		)
	}
	return ZeroLog{}
}

type ZeroLog struct {
}

func (l ZeroLog) LogMode(logger.LogLevel) logger.Interface {
	return l
}
func (l ZeroLog) Info(ctx context.Context, s string, values ...interface{}) {
	logx.Infof(s, values...)
}
func (l ZeroLog) Warn(ctx context.Context, s string, values ...interface{}) {
	logx.Errorf(s, values...)
}
func (l ZeroLog) Error(ctx context.Context, s string, values ...interface{}) {
	logx.Errorf(s, values...)
}
func (l ZeroLog) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
	now := time.Now()
	sql, rows := fc()
	logx.Infof("[%v] [rows:%v] %s", now.Sub(begin).String(), rows, sql)
}