Self \ friendly cast macro.

Hello everyone. Im looking for a macro with such features:
If there is no friendly target or an enemy(harm) then self cast - if there is friendly target then cast on it.
Is this even possible without using default autoself cast option which buggs other addons?
If there is no friendly target or an enemy(harm) then self cast - if there is friendly target then cast on it.
Is this even possible without using default autoself cast option which buggs other addons?