package models

import (
	"tg-mc/utils/database"

	"github.com/sirupsen/logrus"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	TGID   int64
	MCName string
	Status int // 0: pending, 1: normal, 2: banned
}

func init() {
	if err := database.GetDB().AutoMigrate(&User{}); err != nil {
		logrus.Panic(err)
	}
}

func (u *User) TableName() string {
	return "users"
}

func GetUserByTGID(tgID int64) (user User, err error) {
	err = database.GetDB().Where(
		&User{TGID: tgID},
	).First(&user).Error
	return
}

func GetUserByMCName(mcName string) (user User, err error) {
	err = database.GetDB().Where(
		&User{MCName: mcName},
	).First(&user).Error
	return
}

func CreateUserIfNotExist(tgID int64, u *User) (err error) {
	err = database.GetDB().Where(
		&User{TGID: tgID},
	).FirstOrCreate(&u).Error
	return
}

func (u *User) Delete(tgID int64) error {
	return database.GetDB().Where(
		&User{TGID: tgID},
	).Delete(&u).Error
}
