package repositories

import (
	"gorm.io/gorm"
	"strconv"

	"git.dynamicdiscord.de/kalipso/zineshop/models"
)

type ShopItemRepository interface {
	Create(models.ShopItem) (models.ShopItem, error)
	GetAll() ([]models.ShopItem, error)
	GetAllPublic() ([]models.ShopItem, error)
	GetById(string) (models.ShopItem, error)
	GetByTagId(string) ([]models.ShopItem, error)
	GetVariantById(string) (models.ItemVariant, error)
	Update(models.ShopItem) (models.ShopItem, error)
	DeleteById(string) error
}

type GORMShopItemRepository struct {
	DB *gorm.DB
}

func NewGORMShopItemRepository(db *gorm.DB) ShopItemRepository {
	return &GORMShopItemRepository{
		DB: db,
	}
}

func (r *GORMShopItemRepository) Create(shopItem models.ShopItem) (models.ShopItem, error) {
	result := r.DB.Create(&shopItem)
	if result.Error != nil {
		return models.ShopItem{}, result.Error
	}

	return shopItem, nil
}

func (r *GORMShopItemRepository) GetAll() ([]models.ShopItem, error) {
	var shopItems []models.ShopItem
	result := r.DB.Preload("Tags").Preload("Variants").Find(&shopItems)

	return shopItems, result.Error
}

func (r *GORMShopItemRepository) GetAllPublic() ([]models.ShopItem, error) {
	var shopItems []models.ShopItem
	result := r.DB.Preload("Tags").Preload("Variants").Where("is_public = 1").Find(&shopItems)

	return shopItems, result.Error
}

func (r *GORMShopItemRepository) GetById(id string) (models.ShopItem, error) {
	shopItemId, err := strconv.Atoi(id)

	if err != nil {
		return models.ShopItem{}, err
	}

	var shopItem models.ShopItem
	result := r.DB.Preload("Tags").Preload("Variants").First(&shopItem, uint(shopItemId))

	if result.Error != nil {
		return models.ShopItem{}, result.Error
	}

	return shopItem, nil
}

func (r *GORMShopItemRepository) GetByTagId(id string) ([]models.ShopItem, error) {
	tagId, err := strconv.Atoi(id)

	if err != nil {
		return nil, err
	}

	var shopItems []models.ShopItem
	result := r.DB.Joins("JOIN item_tags ON item_tags.shop_item_id = shop_items.id").Where("item_tags.tag_id = ?", tagId).Preload("Tags").Preload("Variants").Find(&shopItems)

	if result.Error != nil {
		return nil, result.Error
	}

	return shopItems, nil
}

func (r *GORMShopItemRepository) GetVariantById(id string) (models.ItemVariant, error) {
	itemVariantId, err := strconv.Atoi(id)

	if err != nil {
		return models.ItemVariant{}, err
	}

	var itemVariant models.ItemVariant
	result := r.DB.First(&itemVariant, uint(itemVariantId))

	if result.Error != nil {
		return models.ItemVariant{}, result.Error
	}

	return itemVariant, nil
}

func (r *GORMShopItemRepository) Update(shopItem models.ShopItem) (models.ShopItem, error) {
	err := r.DB.Model(&shopItem).Association("Tags").Replace(shopItem.Tags)
	if err != nil {
		return models.ShopItem{}, err
	}

	err = r.DB.Model(&shopItem).Association("Variants").Replace(shopItem.Variants)
	if err != nil {
		return models.ShopItem{}, err
	}

	result := r.DB.Save(&shopItem)
	if result.Error != nil {
		return models.ShopItem{}, result.Error
	}

	return shopItem, nil
}

func (r *GORMShopItemRepository) DeleteById(id string) error {
	shopItemId, err := strconv.Atoi(id)

	if err != nil {
		return err
	}

	result := r.DB.Delete(&models.ShopItem{}, shopItemId)
	return result.Error
}
