package entities

import (
	"time"

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

type Candidate 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"`
	Name        string    `gorm:"not null;size:255" json:"name" validate:"required,min=1,max=255"`
	ImageURL    string    `gorm:"size:500" json:"image_url"`
	Description string    `gorm:"type:text" json:"description"`
	CreatedAt   time.Time `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt   time.Time `gorm:"autoUpdateTime" json:"updated_at"`
	VoteEvent   VoteEvent `gorm:"foreignKey:VoteEventID;references:ID" json:"vote_event,omitempty"`
	Votes       []Vote    `gorm:"foreignKey:CandidateID;references:ID" json:"votes,omitempty"`
}

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

func (Candidate) TableName() string {
	return "candidates"
}