package notification

import (
	"context"

	"github.com/rwadurian/mpc-system/pkg/logger"
	"github.com/rwadurian/mpc-system/services/session-coordinator/application/ports/output"
	"go.uber.org/zap"
)

// StubNotificationService is a placeholder implementation of NotificationServicePort
// It logs notification requests but does not actually send them
//
// TODO: Implement real notification adapters:
// - EmailNotificationAdapter: Uses SMTP or email service (SendGrid, SES, etc.)
// - SMSNotificationAdapter: Uses SMS gateway (Twilio, etc.)
// - PushNotificationAdapter: Uses FCM/APNs for mobile push notifications
type StubNotificationService struct{}

// NewStubNotificationService creates a new stub notification service
func NewStubNotificationService() *StubNotificationService {
	return &StubNotificationService{}
}

// SendSessionInvitation logs the invitation but does not send it
// TODO: Implement actual notification sending
func (s *StubNotificationService) SendSessionInvitation(
	ctx context.Context,
	input output.SessionInvitationInput,
) error {
	logger.Info("[TODO] Would send session invitation notification",
		zap.String("party_id", input.PartyID),
		zap.String("session_id", input.SessionID),
		zap.String("session_type", input.SessionType),
		zap.String("email", maskEmail(input.Email)),
		zap.String("phone", maskPhone(input.Phone)),
		zap.Bool("has_push_token", input.PushToken != ""),
		zap.Int64("expires_at", input.ExpiresAt))

	// TODO: Implement actual notification sending:
	// 1. If email is set, send email with session details and join link
	// 2. If phone is set, send SMS with short message and link
	// 3. If push_token is set, send push notification

	return nil
}

// SendSessionReminder logs the reminder but does not send it
// TODO: Implement actual notification sending
func (s *StubNotificationService) SendSessionReminder(
	ctx context.Context,
	input output.SessionReminderInput,
) error {
	logger.Info("[TODO] Would send session reminder notification",
		zap.String("party_id", input.PartyID),
		zap.String("session_id", input.SessionID),
		zap.String("time_remaining", input.TimeRemaining),
		zap.Int("reminder_count", input.ReminderCount))

	return nil
}

// SendSessionExpiredNotification logs the expiration but does not send it
// TODO: Implement actual notification sending
func (s *StubNotificationService) SendSessionExpiredNotification(
	ctx context.Context,
	input output.SessionExpiredInput,
) error {
	logger.Info("[TODO] Would send session expired notification",
		zap.String("party_id", input.PartyID),
		zap.String("session_id", input.SessionID),
		zap.Int64("expired_at", input.ExpiredAt))

	return nil
}

// maskEmail masks an email address for logging (e.g., "j***@example.com")
func maskEmail(email string) string {
	if email == "" {
		return ""
	}
	if len(email) < 3 {
		return "***"
	}
	// Find @ position
	atPos := -1
	for i, c := range email {
		if c == '@' {
			atPos = i
			break
		}
	}
	if atPos <= 0 {
		return "***"
	}
	return email[:1] + "***" + email[atPos:]
}

// maskPhone masks a phone number for logging (e.g., "+1***1234")
func maskPhone(phone string) string {
	if phone == "" {
		return ""
	}
	if len(phone) < 4 {
		return "***"
	}
	// Keep first 2 and last 4 characters
	if len(phone) <= 6 {
		return phone[:1] + "***" + phone[len(phone)-2:]
	}
	return phone[:2] + "***" + phone[len(phone)-4:]
}

// Ensure StubNotificationService implements NotificationServicePort
var _ output.NotificationServicePort = (*StubNotificationService)(nil)
