#!/usr/bin/env python3
import json
import os
import shutil
import subprocess

JOBS_PATH = "/var/lib/docker/volumes/ow404080wwkkgkgc4oswwssc_openclaw-data/_data/.openclaw/cron/jobs.json"
BACKUP = JOBS_PATH + ".bak"

print("📦 Sauvegarde du fichier jobs.json…")
shutil.copy(JOBS_PATH, BACKUP)

with open(JOBS_PATH, "r", encoding="utf-8") as f:
    data = json.load(f)

# Vérifier si le job existe déjà
for job in data["jobs"]:
    if job.get("name") == "turf-scheduler-08h":
        print("ℹ️  Le job turf-scheduler-08h existe déjà. Rien à faire.")
        exit(0)

print("🛠 Ajout du job turf-scheduler-08h…")

new_job = {
    "id": "turf-scheduler-08h",
    "name": "turf-scheduler-08h",
    "enabled": True,
    "schedule": {
        "expr": "0 8 * * *"
    },
    "command": "python3 /home/h3r7/turf_scraper/turf_scheduler.py",
    "timeout": 3600,
    "retries": 0
}

data["jobs"].append(new_job)

with open(JOBS_PATH, "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2, ensure_ascii=False)

print("✅ Job ajouté avec succès.")
print("🔄 Redémarrage d’OpenClaw…")

subprocess.run(["docker", "restart", "openclaw-ow404080wwkkgkgc4oswwssc"])

print("🎉 Terminé !")
