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 (
		messageThread Models.ConversationDetail
		err           error
	)

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

	return messageThread, err
}

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

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

	return messageThread, err
}

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

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

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