package models

import (
	"time"

	"github.com/google/uuid"
)

type User struct {
	ID               uuid.UUID  `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
	Username         string     `gorm:"type:varchar(32);uniqueIndex;not null" json:"username"`
	Email            string     `gorm:"type:varchar(255);uniqueIndex;not null" json:"email"`
	PasswordHash     string     `gorm:"type:text;not null" json:"-"`
	SteamID          *string    `gorm:"type:varchar(20);uniqueIndex" json:"steam_id,omitempty"`
	SteamPersonaName *string    `gorm:"type:varchar(64)" json:"steam_persona_name,omitempty"`
	SteamAvatarURL   *string    `gorm:"type:text" json:"steam_avatar_url,omitempty"`
	SteamLinkedAt    *time.Time `json:"steam_linked_at,omitempty"`
	Role             string     `gorm:"type:varchar(16);not null;default:'user'" json:"role"`
	Tier             string     `gorm:"type:varchar(16);not null;default:'normal'" json:"tier"`
	TierExpiresAt    *time.Time `json:"tier_expires_at,omitempty"`
	ProfileBanner    *string    `gorm:"type:text" json:"profile_banner,omitempty"`
	MMR              int        `gorm:"not null;default:1000" json:"mmr"`
	Wins             int        `gorm:"not null;default:0" json:"wins"`
	Losses           int        `gorm:"not null;default:0" json:"losses"`
	MatchesPlayed    int        `gorm:"not null;default:0" json:"matches_played"`
	CurrentStreak    int        `gorm:"not null;default:0" json:"current_streak"`
	LastMatchAt      *time.Time `json:"last_match_at,omitempty"`
	PlacementDone    bool       `gorm:"not null;default:false" json:"placement_done"`
	CreatedAt        time.Time  `gorm:"not null" json:"created_at"`
	UpdatedAt        time.Time  `gorm:"not null" json:"updated_at"`
}

func (u *User) RoleLevel() int {
	switch u.Role {
	case "owner":
		return 3
	case "admin":
		return 2
	case "moderator":
		return 1
	default:
		return 0
	}
}

func (u *User) TierWeight() float64 {
	switch u.Tier {
	case "vip":
		return 2.0
	case "premium":
		return 1.5
	default:
		return 1.0
	}
}

type PublicProfile struct {
	ID               uuid.UUID  `json:"id"`
	Username         string     `json:"username"`
	SteamPersonaName *string    `json:"steam_persona_name,omitempty"`
	SteamAvatarURL   *string    `json:"steam_avatar_url,omitempty"`
	Role             string     `json:"role"`
	Tier             string     `json:"tier"`
	ProfileBanner    *string    `json:"profile_banner,omitempty"`
	MMR              int        `json:"mmr"`
	Wins             int        `json:"wins"`
	Losses           int        `json:"losses"`
	MatchesPlayed    int        `json:"matches_played"`
	PlacementDone    bool       `json:"placement_done"`
	CreatedAt        time.Time  `json:"created_at"`
}

func (u *User) ToPublicProfile() PublicProfile {
	return PublicProfile{
		ID:               u.ID,
		Username:         u.Username,
		SteamPersonaName: u.SteamPersonaName,
		SteamAvatarURL:   u.SteamAvatarURL,
		Role:             u.Role,
		Tier:             u.Tier,
		ProfileBanner:    u.ProfileBanner,
		MMR:              u.MMR,
		Wins:             u.Wins,
		Losses:           u.Losses,
		MatchesPlayed:    u.MatchesPlayed,
		PlacementDone:    u.PlacementDone,
		CreatedAt:        u.CreatedAt,
	}
}
