package db

import (
	"github.com/tech/sendico/pkg/auth"
	"github.com/tech/sendico/pkg/db/account"
	"github.com/tech/sendico/pkg/db/callbacks"
	"github.com/tech/sendico/pkg/db/chainassets"
	"github.com/tech/sendico/pkg/db/chainwalletroutes"
	mongoimpl "github.com/tech/sendico/pkg/db/internal/mongo"
	"github.com/tech/sendico/pkg/db/invitation"
	"github.com/tech/sendico/pkg/db/organization"
	"github.com/tech/sendico/pkg/db/policy"
	"github.com/tech/sendico/pkg/db/recipient"
	"github.com/tech/sendico/pkg/db/refreshtokens"
	"github.com/tech/sendico/pkg/db/role"
	"github.com/tech/sendico/pkg/db/transaction"
	"github.com/tech/sendico/pkg/db/verification"
	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/mlogger"
)

// Factory exposes high-level repositories used by application services.
type Factory interface {
	NewRefreshTokensDB() (refreshtokens.DB, error)

	NewChainAsstesDB() (chainassets.DB, error)
	NewChainWalletRoutesDB() (chainwalletroutes.DB, error)

	NewAccountDB() (account.DB, error)
	NewOrganizationDB() (organization.DB, error)
	NewInvitationsDB() (invitation.DB, error)
	NewRecipientsDB() (recipient.DB, error)
	NewCallbacksDB() (callbacks.DB, error)
	NewVerificationsDB() (verification.DB, error)

	NewRolesDB() (role.DB, error)
	NewPoliciesDB() (policy.DB, error)

	TransactionFactory() transaction.Factory

	Permissions() auth.Provider

	CloseConnection()
}

// NewConnection builds a Factory backed by the configured driver.
func NewConnection(logger mlogger.Logger, config *Config) (Factory, error) {
	if config.Driver == Mongo {
		return mongoimpl.NewConnection(logger, config.Settings)
	}
	return nil, merrors.InvalidArgument("unknown database driver: "+string(config.Driver), "config.driver")
}
