package psql

import (
	"kurumibot/database"

	"github.com/shopspring/decimal"
)

type Waifu struct {
	ID          int
	Name        string
	Rarity      int
	ExpBonus    decimal.Decimal
	MoneyBonus  decimal.Decimal
	MarketPrice decimal.Decimal
	Fandom      string
	Image       string

	OwnerID int
	Owner   *User
}

func GetAllWaifus() ([]*Waifu, error) {
	waifus := make([]*Waifu, 0)
	tx := database.PostgresDatabase.Joins("Owner").Find(&waifus).Order("id")
	return waifus, tx.Error
}

func GetUserWaifus(userId int) ([]*Waifu, error) {
	waifus := make([]*Waifu, 0)
	tx := database.PostgresDatabase.Find(&waifus, "owner_id = ?", userId).Order("id")
	return waifus, tx.Error
}

func GetFreeWaifus() ([]*Waifu, error) {
	waifus := make([]*Waifu, 0)
	tx := database.PostgresDatabase.Find(&waifus, "owner_id is null").Order("id")
	return waifus, tx.Error
}

func GetFreeWaifusCount() (int64, error) {
	var count int64 = 0
	tx := database.PostgresDatabase.Model(&Waifu{}).Where("owner_id is null").Count(&count)
	return count, tx.Error
}

func GetFreeWaifusWithRarity(rarity int) ([]*Waifu, error) {
	waifus := make([]*Waifu, 0)
	tx := database.PostgresDatabase.Find(&waifus, "owner_id is null and rarity = ?", rarity)
	return waifus, tx.Error
}

func GetWaifuById(id int) (*Waifu, error) {
	waifu := new(Waifu)
	tx := database.PostgresDatabase.Joins("Owner").Find(waifu, id)
	return waifu, tx.Error
}
