import os
import time
import logging
from supabase import create_client

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
)

SUPABASE_URL = os.environ.get("SUPABASE_URL", "https://zxxypgvrszdedsesburc.supabase.co")
SUPABASE_KEY = os.environ.get("SUPABASE_KEY", "sb_publishable_TAZ06230UyO82NqHrFu6Mg_H7QcGxDQ")
INTERVAL_HOURS = int(os.environ.get("INTERVAL_HOURS", "24"))

supabase = create_client(SUPABASE_URL, SUPABASE_KEY)


def ping():
    try:
        supabase.table("xmls").select("id").limit(1).execute()
        logging.info("Ping OK — database is alive.")
    except Exception as e:
        logging.error(f"Ping failed: {e}")


if __name__ == "__main__":
    logging.info(f"Keepalive started. Pinging every {INTERVAL_HOURS}h.")
    while True:
        ping()
        time.sleep(INTERVAL_HOURS * 3600)

