package storage

import (
	"context"
	"time"

	"github.com/tech/sendico/ledger/storage/model"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

type storageError string

func (e storageError) Error() string {
	return string(e)
}

var (
	ErrAccountNotFound       = storageError("ledger.storage: account not found")
	ErrJournalEntryNotFound  = storageError("ledger.storage: journal entry not found")
	ErrBalanceNotFound       = storageError("ledger.storage: balance not found")
	ErrDuplicateIdempotency  = storageError("ledger.storage: duplicate idempotency key")
	ErrInsufficientBalance   = storageError("ledger.storage: insufficient balance")
	ErrAccountFrozen         = storageError("ledger.storage: account is frozen")
	ErrNegativeBalancePolicy = storageError("ledger.storage: negative balance not allowed")
)

type AccountsStore interface {
	Create(ctx context.Context, account *model.Account) error
	Get(ctx context.Context, accountRef primitive.ObjectID) (*model.Account, error)
	GetByAccountCode(ctx context.Context, orgRef primitive.ObjectID, accountCode, currency string) (*model.Account, error)
	GetDefaultSettlement(ctx context.Context, orgRef primitive.ObjectID, currency string) (*model.Account, error)
	ListByOrganization(ctx context.Context, orgRef primitive.ObjectID, limit int, offset int) ([]*model.Account, error)
	UpdateStatus(ctx context.Context, accountRef primitive.ObjectID, status model.AccountStatus) error
}

type JournalEntriesStore interface {
	Create(ctx context.Context, entry *model.JournalEntry) error
	Get(ctx context.Context, entryRef primitive.ObjectID) (*model.JournalEntry, error)
	GetByIdempotencyKey(ctx context.Context, orgRef primitive.ObjectID, idempotencyKey string) (*model.JournalEntry, error)
	ListByOrganization(ctx context.Context, orgRef primitive.ObjectID, limit int, offset int) ([]*model.JournalEntry, error)
}

type PostingLinesStore interface {
	CreateMany(ctx context.Context, lines []*model.PostingLine) error
	ListByJournalEntry(ctx context.Context, entryRef primitive.ObjectID) ([]*model.PostingLine, error)
	ListByAccount(ctx context.Context, accountRef primitive.ObjectID, limit int, offset int) ([]*model.PostingLine, error)
}

type BalancesStore interface {
	Get(ctx context.Context, accountRef primitive.ObjectID) (*model.AccountBalance, error)
	Upsert(ctx context.Context, balance *model.AccountBalance) error
	IncrementBalance(ctx context.Context, accountRef primitive.ObjectID, amount string) error
}

type OutboxStore interface {
	Create(ctx context.Context, event *model.OutboxEvent) error
	ListPending(ctx context.Context, limit int) ([]*model.OutboxEvent, error)
	MarkSent(ctx context.Context, eventRef primitive.ObjectID, sentAt time.Time) error
	MarkFailed(ctx context.Context, eventRef primitive.ObjectID) error
	IncrementAttempts(ctx context.Context, eventRef primitive.ObjectID) error
}
