package jobs

import (
	"time"

	"crussell/auth"
	authHandlers "crussell/handlers/auth"
	"crussell/handlers/scheduling"
	"crussell/handlers/user"
	"crussell/mw"
)

// RegisterAll registers every background maintenance job on the scheduler.
// Call once during server startup, before s.Start().
func RegisterAll(s *Scheduler) {
	// === HIGH FREQUENCY — every 5 minutes ===

	s.Register(Job{
		Name:        "cleanup-reservations",
		Schedule:    "*/5 * * * *",
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     scheduling.CleanupOldReservations,
	})

	s.Register(Job{
		Name:        "cleanup-expired-deposits",
		Schedule:    "*/5 * * * *",
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     scheduling.CleanupExpiredDeposits,
	})

	s.Register(Job{
		Name:        "cleanup-rate-limiters",
		Schedule:    "*/5 * * * *",
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     mw.CleanupAllRateLimiters,
	})

	s.Register(Job{
		Name:        "cleanup-gdpr-export-cache",
		Schedule:    "*/5 * * * *",
		Timeout:     10 * time.Second,
		Concurrency: 1,
		Handler:     user.CleanupGDPRExportCache,
	})

	// === MID FREQUENCY — every minute (progressive rate limiter was on 30s) ===

	s.Register(Job{
		Name:        "cleanup-progressive-rate-limiter",
		Schedule:    "* * * * *",
		Timeout:     10 * time.Second,
		Concurrency: 1,
		Handler:     mw.CleanupProgressiveRateLimiter,
	})

	// === MID FREQUENCY — hourly ===

	s.Register(Job{
		Name:        "cleanup-expired-loyalty-redemptions",
		Schedule:    "0 * * * *",
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     scheduling.CleanupExpiredLoyaltyRedemptions,
	})

	s.Register(Job{
		Name:        "cleanup-old-idempotency-keys",
		Schedule:    "0 * * * *",
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     scheduling.CleanupOldIdempotencyKeys,
	})

	s.Register(Job{
		Name:        "cleanup-revoked-jtis",
		Schedule:    "0 * * * *",
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     auth.CleanupRevokedJTIs,
	})

	s.Register(Job{
		Name:        "cleanup-stale-login-entries",
		Schedule:    "0 * * * *",
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     authHandlers.CleanupStaleLoginEntries,
	})

	// === LOW FREQUENCY — daily, off-peak (staggered to avoid DB contention) ===

	s.Register(Job{
		Name:        "anonymize-stale-guest-accounts",
		Schedule:    "0 3 * * *",
		Timeout:     5 * time.Minute,
		Concurrency: 1,
		Handler:     scheduling.AnonymizeStaleGuestAccounts,
	})

	s.Register(Job{
		Name:        "cleanup-expired-financial-records",
		Schedule:    "0 4 * * *",
		Timeout:     10 * time.Minute,
		Concurrency: 1,
		Handler:     scheduling.CleanupExpiredFinancialRecords,
	})

	s.Register(Job{
		Name:        "cleanup-idle-accounts",
		Schedule:    "30 3 * * *",
		Timeout:     5 * time.Minute,
		Concurrency: 1,
		Handler:     scheduling.CleanupIdleAccounts,
	})

	s.Register(Job{
		Name:        "cleanup-expired-gift-cards",
		Schedule:    "0 5 * * *",
		Timeout:     5 * time.Minute,
		Concurrency: 1,
		Handler:     scheduling.CleanupExpiredGiftCards,
	})

	s.Register(Job{
		Name:        "cleanup-old-name-history",
		Schedule:    "30 4 * * *",
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     scheduling.CleanupOldNameHistory,
	})

	// === BUSINESS LOGIC JOBS ===

	s.Register(Job{
		Name:        "notify-unpaid-1-week",
		Schedule:    "0 7 * * *",          // Daily at 7am — end of business day + 7 days
		Timeout:     2 * time.Minute,
		Concurrency: 1,
		Handler:     scheduling.NotifyUnpaidOneWeek,
	})

	s.Register(Job{
		Name:        "notify-unpaid-1-month",
		Schedule:    "30 7 * * *",          // Daily at 7:30am (staggered from notify-unpaid-1-week)
		Timeout:     2 * time.Minute,
		Concurrency: 1,
		Handler:     scheduling.NotifyUnpaidOneMonth,
	})

	s.Register(Job{
		Name:        "transition-discount-campaigns",
		Schedule:    "0 * * * *",          // Hourly
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     scheduling.TransitionDiscountCampaigns,
	})

	s.Register(Job{
		Name:        "cleanup-verification-codes",
		Schedule:    "0 2 * * *",          // Daily at 2am
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     scheduling.CleanupExpiredVerificationCodes,
	})

	s.Register(Job{
		Name:        "cleanup-refresh-tokens",
		Schedule:    "0 2 * * *",          // Daily at 2am
		Timeout:     30 * time.Second,
		Concurrency: 1,
		Handler:     scheduling.CleanupExpiredRefreshTokens,
	})
}
