package models

import (
	"git.rouggy.com/rouggy/RaceBot/internal/database"
	"github.com/jinzhu/gorm"
)

type Race struct {
	gorm.Model
	Hash        string `gorm:"column:hash;type:varchar(50)" form:"hash" json:"hash" binding:"max=50"`
	TorrentName string `gorm:"column:torrent_name;varchar(100);not null" form:"torrent_name" json:"torrent_name" binding:"required,max=100"`
	Category    string `gorm:"column:category;varchar(50);not null" form:"category" json:"category" binding:"max=50"`
	ContentPath string `gorm:"column:content_path;varchar(100);not null" form:"content_path" json:"content_path" binding:"max=100"`
	RootPath    string `gorm:"column:root_path;varchar(100)" form:"root_path" json:"root_path" binding:"max=100"`
	Size        string `gorm:"column:size;varchar(15)" form:"size" json:"size" binding:"max=15"`
	Won         bool   `gorm:"column:won;bool" form:"won" json:"won"`
}

func NewRace() *Race {
	r := &Race{}
	return r
}

func (r *Race) SaveRace() (*Race, error) {
	db := database.GetDB()
	err := db.Create(&r).Error
	if err != nil {
		return &Race{}, err
	}
	return r, nil
}

func (r *Race) GetRaceByName(title string) (*Race, error) {
	db := database.GetDB()
	err := db.Where(Race{TorrentName: title}).Find(&r).Error
	if err != nil {
		return &Race{}, err
	}
	return r, nil
}