// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package outputs

import (
	"context"
	"encoding/json"

	"github.com/absmach/magistrala/pkg/messaging"
)

type ChannelPublisher struct {
	RePubSub messaging.PubSub `json:"-"`
	Channel  string           `json:"channel"`
	Topic    string           `json:"topic"`
}

func (p *ChannelPublisher) Run(ctx context.Context, msg *messaging.Message, val any) error {
	data, err := json.Marshal(val)
	if err != nil {
		return err
	}

	m := &messaging.Message{
		Domain:    msg.Domain,
		Publisher: msg.Publisher,
		ClientId:  msg.ClientIdentity(),
		Created:   msg.Created,
		Channel:   p.Channel,
		Subtopic:  p.Topic,
		Protocol:  msg.Protocol,
		Payload:   data,
	}

	topic := messaging.EncodeTopicSuffix(msg.Domain, p.Channel, p.Topic)
	if err := p.RePubSub.Publish(ctx, topic, m); err != nil {
		return err
	}

	return nil
}

func (cp *ChannelPublisher) MarshalJSON() ([]byte, error) {
	return json.Marshal(map[string]string{
		"type":    ChannelsType.String(),
		"channel": cp.Channel,
		"topic":   cp.Topic,
	})
}
