package repository

import (
	"context"
	"fmt"

	libmongo "gateway/internal/library/mongo"
)

// EnsureMongoIndexes creates indexes for auth module collections.
func EnsureMongoIndexes(ctx context.Context, conf *libmongo.Conf) error {
	if conf == nil || conf.Host == "" {
		return nil
	}
	if err := ensureInviteIndexes(ctx, conf); err != nil {
		return err
	}
	return ensureRegistrationMetaIndexes(ctx, conf)
}

func ensureInviteIndexes(ctx context.Context, conf *libmongo.Conf) error {
	//nolint:contextcheck // repository ctor pings Mongo at startup without caller ctx
	repo, ok := NewInviteRepository(InviteRepositoryParam{Conf: conf}).(*inviteRepository)
	if !ok {
		return fmt.Errorf("auth: unexpected invite repository type")
	}
	return repo.Index20260521001UP(ctx)
}

func ensureRegistrationMetaIndexes(ctx context.Context, conf *libmongo.Conf) error {
	//nolint:contextcheck // repository ctor pings Mongo at startup without caller ctx
	repo, ok := NewRegistrationMetaRepository(RegistrationMetaRepositoryParam{Conf: conf}).(*registrationMetaRepository)
	if !ok {
		return fmt.Errorf("auth: unexpected registration metadata repository type")
	}
	return repo.Index20260521002UP(ctx)
}
