// journal_entry.go
package ledger

import (
	"time"

	"github.com/tech/sendico/pkg/model"
	"github.com/tech/sendico/pkg/mservice"
	"go.mongodb.org/mongo-driver/v2/bson"
)

// EntryType is a closed set of journal entry kinds.
type EntryType string

const (
	EntryCredit   EntryType = "credit"
	EntryDebit    EntryType = "debit"
	EntryTransfer EntryType = "transfer"
	EntryFX       EntryType = "fx"
	EntryFee      EntryType = "fee"
	EntryAdjust   EntryType = "adjust"
	EntryReverse  EntryType = "reverse"
)

type JournalEntry struct {
	model.PermissionBound `bson:",inline" json:",inline"`

	// Idempotency/de-dup within your chosen scope (e.g., org/request)
	IdempotencyKey string    `bson:"idempotencyKey,omitempty" json:"idempotencyKey,omitempty"`
	EventTime      time.Time `bson:"eventTime"               json:"eventTime"`
	EntryType      EntryType `bson:"entryType"               json:"entryType"`
	Description    string    `bson:"description,omitempty"   json:"description,omitempty"`

	// Monotonic ordering within your chosen scope (e.g., per org/ledger)
	Version int64 `bson:"version" json:"version"`

	// Denormalized set of all affected ledger accounts (for entry-level access control & queries)
	LedgerAccountRefs []bson.ObjectID `bson:"ledgerAccountRefs,omitempty" json:"ledgerAccountRefs,omitempty"`

	// Optional backlink for reversals
	ReversalOf *bson.ObjectID `bson:"reversalOf,omitempty" json:"reversalOf,omitempty"`
}

func (j *JournalEntry) Collection() string {
	return mservice.LedgerEntries
}
