package Database

import (
	"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

// GetConversationDetailByID gets by id
func GetConversationDetailByID(id string) (Models.ConversationDetail, error) {
	var (
		conversationDetail Models.ConversationDetail
		err                error
	)

	err = DB.Preload(clause.Associations).
		Where("id = ?", id).
		First(&conversationDetail).
		Error

	return conversationDetail, err
}

// GetConversationDetailsByIds gets by multiple ids
func GetConversationDetailsByIds(id []string) ([]Models.ConversationDetail, error) {
	var (
		conversationDetail []Models.ConversationDetail
		err                error
	)

	err = DB.Preload(clause.Associations).
		Where("id IN ?", id).
		Find(&conversationDetail).
		Error

	return conversationDetail, err
}

// CreateConversationDetail creates a ConversationDetail record
func CreateConversationDetail(conversationDetail *Models.ConversationDetail) error {
	return DB.Session(&gorm.Session{FullSaveAssociations: true}).
		Create(conversationDetail).
		Error
}

// UpdateConversationDetail updates a ConversationDetail record
func UpdateConversationDetail(conversationDetail *Models.ConversationDetail) error {
	return DB.Session(&gorm.Session{FullSaveAssociations: true}).
		Where("id = ?", conversationDetail.ID).
		Updates(conversationDetail).
		Error
}

// DeleteConversationDetail deletes a ConversationDetail record
func DeleteConversationDetail(conversationDetail *Models.ConversationDetail) error {
	return DB.Session(&gorm.Session{FullSaveAssociations: true}).
		Delete(conversationDetail).
		Error
}
