package tagrepository

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

func (a *accessor) Delete(id string) error {
	var tag database.Tag

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

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

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

	return nil
}
