package ledger

import (
	"time"

	"github.com/tech/sendico/pkg/db/storable"
	"github.com/tech/sendico/pkg/mservice"
)

// Delivery status enum
type OutboxStatus string

const (
	OutboxPending OutboxStatus = "pending"
	OutboxSent    OutboxStatus = "sent"
	OutboxFailed  OutboxStatus = "failed" // terminal after max retries, or keep pending with NextAttemptAt=nil
)

type OutboxEvent struct {
	storable.Base `bson:",inline" json:",inline"`

	EventID        string       `bson:"eventId"        json:"eventId"`                          // deterministic; use as NATS Msg-Id
	Subject        string       `bson:"subject"        json:"subject"`                          // NATS subject / stream routing key
	Payload        []byte       `bson:"payload"        json:"payload"`                          // JSON (or other) payload
	Status         OutboxStatus `bson:"status"         json:"status"`                           // enum
	Attempts       int          `bson:"attempts"       json:"attempts"`                         // total tries
	NextAttemptAt  *time.Time   `bson:"nextAttemptAt,omitempty" json:"nextAttemptAt,omitempty"` // for backoff scheduler
	SentAt         *time.Time   `bson:"sentAt,omitempty"        json:"sentAt,omitempty"`
	LastError      string       `bson:"lastError,omitempty"     json:"lastError,omitempty"`       // brief reason of last failure
	CorrelationRef string       `bson:"correlationRef,omitempty" json:"correlationRef,omitempty"` // e.g., journalEntryRef or idempotencyKey
}

func (o *OutboxEvent) Collection() string {
	return mservice.LedgerOutbox
}
