package service

import (
	_ "time"

	"git.gocasts.ir/ebhomengo/niki/domain/notification/entity"
)

type Notifservice struct {
	MessageBroker MessageBroker
	Repository    Repository
	Channel       Channel
}

type MessageBroker interface {
	AddItem(notification entity.Notification) error
	RemoveItem(notification entity.Notification) error
	HealthCheck() error
}

type Repository interface {
	AddItem(notification entity.Notification) error
}

type Channel interface {
	SendMessage(notification entity.Notification) error
}
type NotificationServiceRequest struct {
	Body      string
	Type      entity.NotificationType
	Recipinet string
}

func (n *Notifservice) NewNotification(r NotificationServiceRequest) *entity.Notification {
	// TODO add validation of notification properties
	return &entity.Notification{
		Type:      r.Type,
		Recipinet: r.Recipinet,
		Body:      r.Body,
		Status:    entity.Pending,
	}
}

func (n *Notifservice) Send(notification entity.Notification) error {
	if err := n.Channel.SendMessage(notification); err != nil {
		return err
	}
	return nil
}

func (n *Notifservice) Add(notification *entity.Notification) error {
	err := n.MessageBroker.AddItem(*notification)
	if err != nil {
		return err
	}
	return nil
}

func (n *Notifservice) Archive(notification *entity.Notification) error {
	if err := n.MessageBroker.RemoveItem(*notification); err != nil {
		return err
	}
	if err := n.Repository.AddItem(*notification); err != nil {
		return err
	}
	return nil
}
