package models

import (
	"time"

	"github.com/google/uuid"
)

type Match struct {
	ID            uuid.UUID  `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
	GameMode      string     `gorm:"type:varchar(32);not null" json:"game_mode"`
	Map           string     `gorm:"type:varchar(64);not null" json:"map"`
	Status        string     `gorm:"type:varchar(32);not null" json:"status"`
	ServerID      *uuid.UUID `gorm:"type:uuid" json:"server_id,omitempty"`
	Team1Name     string     `gorm:"type:varchar(64);default:'Team Alpha'" json:"team1_name"`
	Team2Name     string     `gorm:"type:varchar(64);default:'Team Beta'" json:"team2_name"`
	Team1Score    int        `gorm:"default:0" json:"team1_score"`
	Team2Score    int        `gorm:"default:0" json:"team2_score"`
	ConnectString *string    `gorm:"type:varchar(255)" json:"connect_string,omitempty"`
	CreatedAt     time.Time  `gorm:"not null" json:"created_at"`
	StartedAt     *time.Time `json:"started_at,omitempty"`
	FinishedAt    *time.Time `json:"finished_at,omitempty"`

	Players []MatchPlayer `gorm:"foreignKey:MatchID" json:"players,omitempty"`
	Server  *GameServer   `gorm:"foreignKey:MatchID;references:ID" json:"server,omitempty"`
}

const (
	MatchStatusQueuing           = "queuing"
	MatchStatusMatched           = "matched"
	MatchStatusServerStarting    = "server_starting"
	MatchStatusWaitingForPlayers = "waiting_for_players"
	MatchStatusKnifeRound        = "knife_round"
	MatchStatusSideSelection     = "side_selection"
	MatchStatusLive              = "live"
	MatchStatusFinished          = "finished"
	MatchStatusCancelled         = "cancelled"
	MatchStatusAborted           = "aborted"
	MatchStatusCleanup           = "cleanup"
)

type MatchPlayer struct {
	ID       uuid.UUID  `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
	MatchID  uuid.UUID  `gorm:"type:uuid;not null;index" json:"match_id"`
	UserID   uuid.UUID  `gorm:"type:uuid;not null;index" json:"user_id"`
	Team     int        `gorm:"type:smallint;not null" json:"team"`
	Result   *string    `gorm:"type:varchar(8)" json:"result,omitempty"`
	MMRDelta *int       `json:"mmr_delta,omitempty"`

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

type MatchPlayerStats struct {
	ID           uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
	MatchID      uuid.UUID `gorm:"type:uuid;not null;uniqueIndex:idx_mps_match_user" json:"match_id"`
	UserID       uuid.UUID `gorm:"type:uuid;not null;uniqueIndex:idx_mps_match_user" json:"user_id"`
	Kills        int       `gorm:"default:0" json:"kills"`
	Deaths       int       `gorm:"default:0" json:"deaths"`
	Assists      int       `gorm:"default:0" json:"assists"`
	Headshots    int       `gorm:"default:0" json:"headshots"`
	Damage       int       `gorm:"default:0" json:"damage"`
	RoundsPlayed int       `gorm:"default:0" json:"rounds_played"`
	MVPs         int       `gorm:"default:0" json:"mvps"`
}
