package model

import "encoding/json"

type Role string

const (
	RoleProducer   Role = "producer"
	RoleConsumer   Role = "consumer"
	RoleController Role = "controller"
)

type Envelope struct {
	SchemaVersion int             `json:"schemaVersion"`
	MessageID     string          `json:"messageId,omitempty"`
	Timestamp     int64           `json:"timestamp"`
	Topic         string          `json:"topic"`
	Source        Source          `json:"source"`
	Target        Target          `json:"target"`
	Payload       json.RawMessage `json:"payload"`
}

type Source struct {
	Kind Role   `json:"kind"`
	ID   string `json:"id"`
	Mode string `json:"mode,omitempty"`
}

type Target struct {
	ChannelID string `json:"channelId,omitempty"`
	DeviceID  string `json:"deviceId,omitempty"`
	GroupID   string `json:"groupId,omitempty"`
}

type ClientMessage struct {
	Type          string         `json:"type"`
	Role          Role           `json:"role,omitempty"`
	ChannelID     string         `json:"channelId,omitempty"`
	Token         string         `json:"token,omitempty"`
	Subscriptions []Subscription `json:"subscriptions,omitempty"`
	Envelope      *Envelope      `json:"envelope,omitempty"`
}

type Subscription struct {
	ChannelID string `json:"channelId,omitempty"`
	DeviceID  string `json:"deviceId,omitempty"`
	GroupID   string `json:"groupId,omitempty"`
	Topic     string `json:"topic,omitempty"`
}

type ServerMessage struct {
	Type      string          `json:"type"`
	SessionID string          `json:"sessionId,omitempty"`
	Error     string          `json:"error,omitempty"`
	Envelope  *Envelope       `json:"envelope,omitempty"`
	State     json.RawMessage `json:"state,omitempty"`
}

type LatestState struct {
	DeviceID  string   `json:"deviceId"`
	GroupID   string   `json:"groupId,omitempty"`
	UpdatedAt int64    `json:"updatedAt"`
	Topics    []string `json:"topics"`
}
