package gateway

import (
	"context"

	"github.com/tech/sendico/gateway/mntx/storage"
	"github.com/tech/sendico/gateway/mntx/storage/model"
)

// mockRepository implements storage.Repository for tests.
type mockRepository struct {
	payouts *cardPayoutStore
}

func newMockRepository() *mockRepository {
	return &mockRepository{
		payouts: newCardPayoutStore(),
	}
}

func (r *mockRepository) Payouts() storage.PayoutsStore {
	return r.payouts
}

// cardPayoutStore implements storage.PayoutsStore for tests.
type cardPayoutStore struct {
	data map[string]*model.CardPayout
}

func newCardPayoutStore() *cardPayoutStore {
	return &cardPayoutStore{
		data: make(map[string]*model.CardPayout),
	}
}

func (s *cardPayoutStore) FindByIdempotencyKey(_ context.Context, key string) (*model.CardPayout, error) {
	for _, v := range s.data {
		if v.IdempotencyKey == key {
			return v, nil
		}
	}
	return nil, nil
}

func (s *cardPayoutStore) FindByPaymentID(_ context.Context, id string) (*model.CardPayout, error) {
	v, ok := s.data[id]
	if !ok {
		return nil, nil
	}
	return v, nil
}

func (s *cardPayoutStore) Upsert(_ context.Context, record *model.CardPayout) error {
	s.data[record.PayoutID] = record
	return nil
}

// Save is a helper for tests to pre-populate data.
func (s *cardPayoutStore) Save(state *model.CardPayout) {
	s.data[state.PayoutID] = state
}

// Get is a helper for tests to retrieve data.
func (s *cardPayoutStore) Get(id string) (*model.CardPayout, bool) {
	v, ok := s.data[id]
	return v, ok
}
