package categoryrepository

import (
	"fmt"
	"legalgo-BE-go/database"
)

func (a *accessor) Delete(id string) error {
	var category database.Category

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

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

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

	return nil
}
