package events

import (
	"context"
	"time"
)

// WhatsAppConnectedEvent creates a WhatsApp connected event
func WhatsAppConnectedEvent(ctx context.Context, accountID string, phoneNumber string) Event {
	return NewEvent(ctx, EventWhatsAppConnected, map[string]any{
		"account_id":   accountID,
		"phone_number": phoneNumber,
	})
}

// WhatsAppDisconnectedEvent creates a WhatsApp disconnected event
func WhatsAppDisconnectedEvent(ctx context.Context, accountID string, reason string) Event {
	return NewEvent(ctx, EventWhatsAppDisconnected, map[string]any{
		"account_id": accountID,
		"reason":     reason,
	})
}

// WhatsAppPairSuccessEvent creates a WhatsApp pair success event
func WhatsAppPairSuccessEvent(ctx context.Context, accountID string) Event {
	return NewEvent(ctx, EventWhatsAppPairSuccess, map[string]any{
		"account_id": accountID,
	})
}

// WhatsAppPairFailedEvent creates a WhatsApp pair failed event
func WhatsAppPairFailedEvent(ctx context.Context, accountID string, err error) Event {
	return NewEvent(ctx, EventWhatsAppPairFailed, map[string]any{
		"account_id": accountID,
		"error":      err.Error(),
	})
}

// WhatsAppQRCodeEvent creates a WhatsApp QR code event
func WhatsAppQRCodeEvent(ctx context.Context, accountID string, qrCode string, qrURL string) Event {
	return NewEvent(ctx, EventWhatsAppQRCode, map[string]any{
		"account_id": accountID,
		"qr_code":    qrCode,
		"qr_url":     qrURL,
	})
}

// WhatsAppQRTimeoutEvent creates a WhatsApp QR timeout event
func WhatsAppQRTimeoutEvent(ctx context.Context, accountID string) Event {
	return NewEvent(ctx, EventWhatsAppQRTimeout, map[string]any{
		"account_id": accountID,
	})
}

// WhatsAppQRErrorEvent creates a WhatsApp QR error event
func WhatsAppQRErrorEvent(ctx context.Context, accountID string, err error) Event {
	return NewEvent(ctx, EventWhatsAppQRError, map[string]any{
		"account_id": accountID,
		"error":      err.Error(),
	})
}

// WhatsAppPairEventGeneric creates a generic WhatsApp pairing event
func WhatsAppPairEventGeneric(ctx context.Context, accountID string, eventName string, data map[string]any) Event {
	eventData := map[string]any{
		"account_id": accountID,
		"event":      eventName,
	}
	for k, v := range data {
		eventData[k] = v
	}
	return NewEvent(ctx, EventWhatsAppPairEvent, eventData)
}

// MessageReceivedEvent creates a message received event
func MessageReceivedEvent(ctx context.Context, accountID, messageID, from, to, text string, timestamp time.Time, isGroup bool, groupName, senderName, messageType, mimeType, filename, mediaBase64, mediaURL string) Event {
	return NewEvent(ctx, EventMessageReceived, map[string]any{
		"account_id":   accountID,
		"message_id":   messageID,
		"from":         from,
		"to":           to,
		"text":         text,
		"timestamp":    timestamp,
		"is_group":     isGroup,
		"group_name":   groupName,
		"sender_name":  senderName,
		"message_type": messageType,
		"mime_type":    mimeType,
		"filename":     filename,
		"media_base64": mediaBase64,
		"media_url":    mediaURL,
	})
}

// MessageSentEvent creates a message sent event
func MessageSentEvent(ctx context.Context, accountID, messageID, to, text string) Event {
	return NewEvent(ctx, EventMessageSent, map[string]any{
		"account_id": accountID,
		"message_id": messageID,
		"to":         to,
		"text":       text,
	})
}

// MessageFailedEvent creates a message failed event
func MessageFailedEvent(ctx context.Context, accountID, to, text string, err error) Event {
	return NewEvent(ctx, EventMessageFailed, map[string]any{
		"account_id": accountID,
		"to":         to,
		"text":       text,
		"error":      err.Error(),
	})
}

// MessageDeliveredEvent creates a message delivered event
func MessageDeliveredEvent(ctx context.Context, accountID, messageID, from string, timestamp time.Time) Event {
	return NewEvent(ctx, EventMessageDelivered, map[string]any{
		"account_id": accountID,
		"message_id": messageID,
		"from":       from,
		"timestamp":  timestamp,
	})
}

// MessageReadEvent creates a message read event
func MessageReadEvent(ctx context.Context, accountID, messageID, from string, timestamp time.Time) Event {
	return NewEvent(ctx, EventMessageRead, map[string]any{
		"account_id": accountID,
		"message_id": messageID,
		"from":       from,
		"timestamp":  timestamp,
	})
}

// TemplateStatusUpdateEvent creates a template status update event
func TemplateStatusUpdateEvent(ctx context.Context, accountID, templateName, templateID, language, status string, rejectionReasons []string) Event {
	return NewEvent(ctx, EventTemplateStatusUpdate, map[string]any{
		"account_id":        accountID,
		"template_name":     templateName,
		"template_id":       templateID,
		"language":          language,
		"status":            status,
		"rejection_reasons": rejectionReasons,
	})
}

// HookTriggeredEvent creates a hook triggered event
func HookTriggeredEvent(ctx context.Context, hookID, hookName, url string, payload any) Event {
	return NewEvent(ctx, EventHookTriggered, map[string]any{
		"hook_id":   hookID,
		"hook_name": hookName,
		"url":       url,
		"payload":   payload,
	})
}

// HookSuccessEvent creates a hook success event
func HookSuccessEvent(ctx context.Context, hookID, hookName string, statusCode int, response any) Event {
	return NewEvent(ctx, EventHookSuccess, map[string]any{
		"hook_id":     hookID,
		"hook_name":   hookName,
		"status_code": statusCode,
		"response":    response,
	})
}

// HookFailedEvent creates a hook failed event
func HookFailedEvent(ctx context.Context, hookID, hookName string, err error) Event {
	return NewEvent(ctx, EventHookFailed, map[string]any{
		"hook_id":   hookID,
		"hook_name": hookName,
		"error":     err.Error(),
	})
}
