package bootstrap

import (
	"context"
	"fmt"

	"gorm.io/gorm"

	"github.com/pocket-id/pocket-id/backend/internal/job"
)

func registerScheduledJobs(ctx context.Context, db *gorm.DB, svc *services, scheduler *job.Scheduler) error {
	err := scheduler.RegisterLdapJobs(ctx, svc.ldapService, svc.appConfigService)
	if err != nil {
		return fmt.Errorf("failed to register LDAP jobs in scheduler: %w", err)
	}
	err = scheduler.RegisterGeoLiteUpdateJobs(ctx, svc.geoLiteService)
	if err != nil {
		return fmt.Errorf("failed to register GeoLite DB update service: %w", err)
	}
	err = scheduler.RegisterDbCleanupJobs(ctx, db)
	if err != nil {
		return fmt.Errorf("failed to register DB cleanup jobs in scheduler: %w", err)
	}
	err = scheduler.RegisterApiKeyExpiryJob(ctx, svc.apiKeyModule, svc.appConfigService, svc.emailService)
	if err != nil {
		return fmt.Errorf("failed to register API key expiration jobs in scheduler: %w", err)
	}
	err = scheduler.RegisterScimJobs(ctx, svc.scimService)
	if err != nil {
		return fmt.Errorf("failed to register SCIM scheduler job: %w", err)
	}

	return nil
}
