package unc

import (
	"fmt"
	"strconv"

	"github.com/pingcap/dbaas/billing2/handler/novuhandler"

	"github.com/pingcap/dbaas/billing2/constant"
	"github.com/pingcap/dbaas/billing2/handler/centralhandler"
	"github.com/pingcap/dbaas/billing2/handler/eventbushandler"
	"github.com/pingcap/dbaas/billing2/utils/log"
	"go.uber.org/zap"
)

type NotificationModel struct{}

type AddUserToOrgEventData struct {
	OrgID  uint64 `json:"org_id"`
	UserID uint64 `json:"user_id"`
	Role   string `json:"role"`
}
type RemoveUserFromOrgEventData struct {
	OrgID  uint64 `json:"org_id"`
	UserID uint64 `json:"user_id"`
}

func (model *NotificationModel) CreateNotificationSubscribers(orgEventData *AddUserToOrgEventData) error {
	log.Info("CreateNotificationSubscribers group  ", zap.Any("orgEventData", orgEventData))
	err := model.CreateNovuSubscribersByTenantID(uint(orgEventData.OrgID))
	if err != nil {
		log.Error("CreateNotificationSubscribers group  error", zap.Error(err), zap.Any("orgEventData", orgEventData))
		return err

	}
	log.Info("CreateNotificationSubscribers group success ", zap.Any("orgEventData", orgEventData))
	return nil
}
func (model *NotificationModel) CreateNovuSubscribersByTenantID(tenantID uint) error {
	userList, err := centralhandler.GetCentralHandler().GetUserListByOrgID(tenantID,
		[]string{constant.RoleOrgOwner, constant.RoleOrgMember, constant.RoleOrgBillingAdmin, constant.RoleOrgAuditAdmin, constant.RoleOrgBillingingViewer})
	if err != nil {
		return err
	}
	var input []map[string]string
	for _, user := range userList {
		sub := make(map[string]string)
		subscriberID := fmt.Sprintf("%s_%s", strconv.FormatUint(uint64(tenantID), 10), strconv.FormatUint(uint64(user.UserID), 10))
		sub[novuhandler.CreateSubscriberSubscriberId] = subscriberID
		if user.FirstName != "" {
			sub[novuhandler.CreateSubscriberFirstName] = user.FirstName
		}
		if user.LastName != "" {
			sub[novuhandler.CreateSubscriberLastName] = user.LastName
		}
		if user.Email != "" {
			sub[novuhandler.CreateSubscriberEmail] = user.Email
		}
		input = append(input, sub)
	}
	if input == nil {
		log.Info("CreateNovuSubscribersByTenantID is nil   ", zap.Uint("tenantID", tenantID))
		return nil
	}
	return novuhandler.Get().CreateSubscribers(input)
}

func (model *NotificationModel) DeleteNotificationSubscribers(orgEventData *RemoveUserFromOrgEventData) error {
	log.Info("DeleteNotificationSubscribers group  ", zap.Any("orgEventData", orgEventData))

	return nil
}
func (model *NotificationModel) RegisterEventHandler() error {
	eventHandlerFuncAdd := func(event *eventbushandler.Event) error {
		return model.CreateNotificationSubscribers(event.EventData.(*AddUserToOrgEventData))
	}
	err := eventbushandler.Get().RegisterEventHandleFunc(constant.AddUserToOrgEventType, "billing_unc", AddUserToOrgEventData{}, eventHandlerFuncAdd)
	if err != nil {
		return err
	}
	eventHandlerFuncDel := func(event *eventbushandler.Event) error {
		return model.DeleteNotificationSubscribers(event.EventData.(*RemoveUserFromOrgEventData))
	}
	err = eventbushandler.Get().RegisterEventHandleFunc(constant.RemoveUserFromOrgEventType, "billing_unc", RemoveUserFromOrgEventData{}, eventHandlerFuncDel)
	if err != nil {
		return err
	}
	return nil
}
