package usecase

import "context"

// AppNotifWriter is satisfied by appnotif usecase for system notifications.
type AppNotifWriter interface {
	// InsertSystem creates a one-shot system notification.
	InsertSystem(ctx context.Context, ownerUID int64, title, body, refType, refID string) error
}

// NotifierFromAppNotif builds SweepNotifier from appnotif bridge.
func NotifierFromAppNotif(w AppNotifWriter) SweepNotifier {
	return sweepNotifyFunc(func(ctx context.Context, ownerUID int64, sweepID, watchID, reason string) error {
		if w == nil {
			return nil
		}
		title := "雷達巡檢失敗"
		body := reason
		if body == "" {
			body = "今天的雷達巡檢沒有完成，請稍後重試或檢查抓取設定。"
		}
		return w.InsertSystem(ctx, ownerUID, title, body, "sweep", sweepID)
	})
}

type sweepNotifyFunc func(ctx context.Context, ownerUID int64, sweepID, watchID, reason string) error

func (f sweepNotifyFunc) NotifySweepFailed(ctx context.Context, ownerUID int64, sweepID, watchID, reason string) error {
	return f(ctx, ownerUID, sweepID, watchID, reason)
}
