//go:build ignore
// NOTE: requires wails dependency in target project — use this file by copying into a Wails project

package infra

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	"github.com/wailsapp/wails/v2/pkg/runtime"
)

// WailsEventPayload es un envelope para eventos tipados.
type WailsEventPayload struct {
	Type      string      `json:"type"`
	Data      interface{} `json:"data"`
	Timestamp time.Time   `json:"timestamp"`
}

// WailsEmitEvent emite un evento tipado al frontend.
func WailsEmitEvent(ctx context.Context, eventName string, data interface{}) {
	payload := WailsEventPayload{
		Type:      eventName,
		Data:      data,
		Timestamp: time.Now(),
	}
	runtime.EventsEmit(ctx, eventName, payload)
}

// WailsEmitJSON emite datos como JSON string al frontend.
func WailsEmitJSON(ctx context.Context, eventName string, data interface{}) error {
	b, err := json.Marshal(data)
	if err != nil {
		return fmt.Errorf("marshaling event data: %w", err)
	}
	runtime.EventsEmit(ctx, eventName, string(b))
	return nil
}
