package model

import (
	"opencatd-open/internal/consts"
	"time"
)

type User struct {
	ID             int64            `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
	Name           string           `json:"name" gorm:"column:name;index"`
	Username       string           `json:"username" gorm:"column:username;unique;index"`
	Password       string           `json:"-" gorm:"column:password;"`
	NewPassword    string           `json:"newpassword" gorm:"-"`
	Role           *consts.UserRole `json:"role" gorm:"column:role;type:int;default:0"` // default user 0-10-20
	Active         *bool            `json:"active" gorm:"column:active;default:true;"`
	Status         int              `json:"status" gorm:"column:status;type:int;default:1"` // disabled 0, enabled 1, deleted 2
	AvatarURL      string           `json:"avatar_url" gorm:"column:avatar_url;type:varchar(255)"`
	EmailVerified  *bool            `json:"email_verified" gorm:"column:email_verified;default:false"`
	Email          string           `json:"email" gorm:"column:email;type:varchar(255);index"`
	Quota          *float32         `json:"quota" gorm:"column:quota;bigint;default:0"`                  // default unlimited
	UsedQuota      *float32         `json:"used_quota" gorm:"column:used_quota;bigint;default:0"`        // default 0
	UnlimitedQuota *bool            `json:"unlimited_quota" gorm:"column:unlimited_quota;default:true;"` // 0 limited , 1 unlimited
	Timezone       string           `json:"timezone" gorm:"column:timezone;type:varchar(50)"`
	Language       string           `json:"language" gorm:"column:language;type:varchar(50)"`

	// 添加一对多关系
	Tokens   []Token   `json:"tokens" gorm:"foreignKey:UserID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
	Passkeys []Passkey `json:"passkeys" gorm:"foreignKey:UserID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`

	CreatedAt int64 `json:"created_at,omitempty" gorm:"autoCreateTime"`
	UpdatedAt int64 `json:"updated_at,omitempty" gorm:"autoUpdateTime"`
}

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

type Session struct {
	ID           int64     `json:"id" gorm:"primaryKey;autoIncrement"`
	UserID       int64     `json:"user_id" gorm:"index:idx_user_id"`
	Token        string    `json:"token" gorm:"type:varchar(64);uniqueIndex"`
	DeviceType   string    `json:"device_type" gorm:"type:varchar(100);default:''"`
	DeviceName   string    `json:"device_name" gorm:"type:varchar(100);default:''"`
	LastActiveAt time.Time `json:"last_active_at" gorm:"type:timestamp;default:CURRENT_TIMESTAMP"`
	LogoutAt     time.Time `json:"logout_at" gorm:"type:timestamp;null"`

	CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
	UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP;update:CURRENT_TIMESTAMP"`
}

func (Session) TableName() string {
	return "sessions"
}
