package entities

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type Vote struct {
	ID          uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	VoteEventID uuid.UUID `gorm:"type:uuid;not null" json:"vote_event_id"`
	CandidateID uuid.UUID `gorm:"type:uuid;not null" json:"candidate_id"`
	UserID      uuid.UUID `gorm:"type:uuid;not null" json:"user_id"`
	CreatedAt   time.Time `gorm:"autoCreateTime" json:"created_at"`
	VoteEvent   VoteEvent `gorm:"foreignKey:VoteEventID;references:ID" json:"vote_event,omitempty"`
	Candidate   Candidate `gorm:"foreignKey:CandidateID;references:ID" json:"candidate,omitempty"`
	User        User      `gorm:"foreignKey:UserID;references:ID" json:"user,omitempty"`
}

func (v *Vote) BeforeCreate(tx *gorm.DB) error {
	if v.ID == uuid.Nil {
		v.ID = uuid.New()
	}
	return nil
}

func (Vote) TableName() string {
	return "votes"
}