package utils

import (
	"context"
	"github.com/jackc/pgx/v5/pgxpool"
)

// DEPRECATED: Use config.AppSettings instead.
// These functions remain for backward compatibility but no longer require DB queries.
// They now return empty values; new code should use cfg.AppSettings directly.

func GetSetting(ctx context.Context, pool *pgxpool.Pool, key, fallback string) string {
	var value string
	err := pool.QueryRow(ctx,
		"SELECT setting_value FROM settings WHERE setting_key = $1 LIMIT 1", key,
	).Scan(&value)

	if err != nil || value == "" {
		return fallback
	}
	return value
}

func GetBoolSetting(ctx context.Context, pool *pgxpool.Pool, key string, fallback bool) bool {
	val := GetSetting(ctx, pool, key, "")
	if val == "" {
		return fallback
	}
	return val == "true" || val == "1" || val == "yes" || val == "on"
}

// kept for legacy callers that haven't been migrated yet
func GetAuthMethodSettings(ctx context.Context, pool *pgxpool.Pool) map[string]bool {
	return map[string]bool{
		"passwordEnabled":       GetBoolSetting(ctx, pool, "login_password_enabled", true),
		"googleEnabled":         GetBoolSetting(ctx, pool, "login_google_enabled", true),
		"googleRegisteredOnly":  GetBoolSetting(ctx, pool, "google_login_registered_only", true),
		"registrationEnabled":   GetBoolSetting(ctx, pool, "registration_enabled", true),
	}
}
