package newsrepository

import (
	"fmt"
	newsdomain "legalgo-BE-go/internal/domain/news"
)

func (a *accessor) Delete(id string) error {
	var news newsdomain.News

	if err := a.db.Preload("Categories").Preload("Tags").First(&news, "id = ?", id).Error; err != nil {
		return fmt.Errorf("failed to find news: %v", err)
	}

	if err := a.db.Model(&news).Association("Categories").Clear(); err != nil {
		return fmt.Errorf("failed to remove categories association: %v", err)
	}

	if err := a.db.Model(&news).Association("Tags").Clear(); err != nil {
		return fmt.Errorf("failed to remove tags association: %v", err)
	}

	if err := a.db.Delete(&news, "id = ?", id).Error; err != nil {
		return fmt.Errorf("failed to delete news %s : %v", id, err)
	}

	return nil
}
