package storage

import (
	"context"
	"time"

	"github.com/tech/sendico/ledger/storage/model"
	pkm "github.com/tech/sendico/pkg/model"
	"github.com/tech/sendico/pkg/model/account_role"
	"go.mongodb.org/mongo-driver/v2/bson"
)

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")
)

// AccountsFilter describes optional filter parameters for listing accounts.
type AccountsFilter struct {
	// OwnerRefFilter is a 3-state filter:
	// - nil: no filter on owner_ref (return all)
	// - pointer to zero ObjectID: filter for accounts where owner_ref is nil
	// - pointer to a value: filter for accounts where owner_ref matches
	OwnerRefFilter *bson.ObjectID
}

type AccountsStore interface {
	Create(ctx context.Context, account *pkm.LedgerAccount) error
	Get(ctx context.Context, accountRef bson.ObjectID) (*pkm.LedgerAccount, error)
	GetByAccountCode(ctx context.Context, orgRef bson.ObjectID, accountCode, currency string) (*pkm.LedgerAccount, error)
	GetByRole(ctx context.Context, orgRef bson.ObjectID, currency string, role account_role.AccountRole) (*pkm.LedgerAccount, error)
	GetSystemAccount(ctx context.Context, purpose pkm.SystemAccountPurpose, currency string) (*pkm.LedgerAccount, error)
	GetDefaultSettlement(ctx context.Context, orgRef bson.ObjectID, currency string) (*pkm.LedgerAccount, error)
	ListByOrganization(ctx context.Context, orgRef bson.ObjectID, filter *AccountsFilter, limit int, offset int) ([]*pkm.LedgerAccount, error)
	UpdateStatus(ctx context.Context, accountRef bson.ObjectID, status pkm.LedgerAccountStatus) error
}

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

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

type BalancesStore interface {
	Get(ctx context.Context, accountRef bson.ObjectID) (*model.AccountBalance, error)
	Upsert(ctx context.Context, balance *model.AccountBalance) error
	IncrementBalance(ctx context.Context, accountRef bson.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 bson.ObjectID, sentAt time.Time) error
	MarkFailed(ctx context.Context, eventRef bson.ObjectID) error
	IncrementAttempts(ctx context.Context, eventRef bson.ObjectID) error
}
