package models

import (
	"time"

	"github.com/google/uuid"
)

type Ban struct {
	ID           uuid.UUID  `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
	UserID       uuid.UUID  `gorm:"type:uuid;not null;index" json:"user_id"`
	IssuedBy     uuid.UUID  `gorm:"type:uuid;not null" json:"issued_by"`
	Type         string     `gorm:"type:varchar(16);not null" json:"type"`
	Reason       string     `gorm:"type:text;not null" json:"reason"`
	InternalNote *string    `gorm:"type:text" json:"internal_note,omitempty"`
	Evidence     *string    `gorm:"type:text" json:"evidence,omitempty"`
	Duration     *string    `gorm:"type:varchar(64)" json:"duration,omitempty"`
	StartsAt     time.Time  `gorm:"not null" json:"starts_at"`
	ExpiresAt    *time.Time `json:"expires_at,omitempty"`
	Revoked      bool       `gorm:"default:false" json:"revoked"`
	RevokedBy    *uuid.UUID `gorm:"type:uuid" json:"revoked_by,omitempty"`
	RevokedAt    *time.Time `json:"revoked_at,omitempty"`
	RevokeReason *string    `gorm:"type:text" json:"revoke_reason,omitempty"`
	CreatedAt    time.Time  `gorm:"not null" json:"created_at"`

	User   User `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Issuer User `gorm:"foreignKey:IssuedBy" json:"issuer,omitempty"`
}

const (
	BanTypeWarning = "warning"
	BanTypeTemp    = "temp"
	BanTypePerm    = "perm"
	BanTypeGame    = "game"
	BanTypeFull    = "full"
)

func (b *Ban) IsActive() bool {
	if b.Revoked {
		return false
	}
	if b.Type == BanTypeWarning {
		return false
	}
	if b.ExpiresAt != nil && b.ExpiresAt.Before(time.Now()) {
		return false
	}
	return true
}

type StaffNote struct {
	ID        uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
	UserID    uuid.UUID `gorm:"type:uuid;not null;index" json:"user_id"`
	AuthorID  uuid.UUID `gorm:"type:uuid;not null" json:"author_id"`
	Content   string    `gorm:"type:text;not null" json:"content"`
	CreatedAt time.Time `gorm:"not null" json:"created_at"`

	Author User `gorm:"foreignKey:AuthorID" json:"author,omitempty"`
}
