package middleware

import (
	"log/slog"

	"github.com/gin-gonic/gin"
	"git.mchus.pro/mchus/quoteforge/internal/db"
	"git.mchus.pro/mchus/quoteforge/internal/localdb"
)

// OfflineDetector creates middleware that detects offline mode
// Sets context values:
//   - "is_offline" (bool) - true if MariaDB is unavailable
//   - "localdb" (*localdb.LocalDB) - local database instance for fallback
func OfflineDetector(connMgr *db.ConnectionManager, local *localdb.LocalDB) gin.HandlerFunc {
	return func(c *gin.Context) {
		isOffline := !connMgr.IsOnline()

		// Set context values for handlers
		c.Set("is_offline", isOffline)
		c.Set("localdb", local)

		if isOffline {
			slog.Debug("offline mode detected - MariaDB unavailable")
		}

		c.Next()
	}
}
