package Admin

import (
	dto "astrology-api/dto_admin"
	"astrology-api/services_admin"
	"astrology-api/utils"

	"github.com/gin-gonic/gin"
)

// Push notifications the admin panel triggers: a message an admin writes, and
// the notifications for decisions the panel itself makes (profile
// verification, withdrawal approval).
type NotificationController struct {
	notificationService services_admin.NotificationService
}

func NewNotificationController(
	notificationService services_admin.NotificationService,
) *NotificationController {

	return &NotificationController{
		notificationService: notificationService,
	}
}

// Send delivers an admin-written message to one customer, one astrologer, or
// everybody in either audience.
//
// Delivery is asynchronous: a 200 means the notification was queued and the
// in-app rows were written, not that every handset has it.
//
// POST /api/admin/notification/send
func (c *NotificationController) Send(ctx *gin.Context) {

	var request dto.SendNotificationRequest

	if err := ctx.ShouldBindJSON(&request); err != nil {
		utils.BadRequest(ctx, err.Error())
		return
	}

	response, err := c.notificationService.Send(request)

	if err != nil {
		utils.BadRequest(ctx, err.Error())
		return
	}

	utils.Success(ctx, "Notification queued", response)
}

// NotifyEvent sends the notification for a decision the panel handled —
// PROFILE_APPROVED, PROFILE_REJECTED, WITHDRAW_APPROVED, WITHDRAW_REJECTED.
//
// The panel calls this after it has made the change in the database, so the
// astrologer is told in the same words the apps use everywhere else.
//
// POST /api/admin/notification/event
func (c *NotificationController) NotifyEvent(ctx *gin.Context) {

	var request dto.NotifyEventRequest

	if err := ctx.ShouldBindJSON(&request); err != nil {
		utils.BadRequest(ctx, err.Error())
		return
	}

	response, err := c.notificationService.NotifyEvent(request)

	if err != nil {
		utils.BadRequest(ctx, err.Error())
		return
	}

	utils.Success(ctx, "Notification queued", response)
}
