package service

import (
	"bpmsapi/dto"
	"bpmsapi/entity"
	"bpmsapi/repository"
)

type Notificationusecase interface {
	UpdateNotificationToken(input entity.UpdateNotificationTokenInput) entity.CustomResponse
}

type NotificationService struct {
	notificationRepository *repository.NotificationRepository
}

func NotificationServiceInit(notificationRepository *repository.NotificationRepository) *NotificationService {
	return &NotificationService{
		notificationRepository: notificationRepository,
	}
}

func (notificationServ *NotificationService) UpdateNotificationToken(input entity.UpdateNotificationTokenInput) (response entity.CustomResponse) {
	tx, errt := notificationServ.notificationRepository.Begin()
	if errt != nil {
		response.Resultcode = 1
		response.Message = errt.Error()
	}

	resp, err := notificationServ.notificationRepository.NotificationTokenUpdate(tx, input)
	if err != nil {
		tx.Rollback()
		response.Resultcode = 1
		response.Message = dto.ErrorMessageUpdateToken + err.Error()
	} else {
		commitErr := tx.Commit()
		if commitErr != nil {
			response.Resultcode = 1
			response.Message = dto.ErrorMessageUpdateToken + err.Error()
		} else {
			response.Resultcode = 0
			response.Message = resp
	

		}
	}
	return response
}
