import os,shutil
import time
import subprocess
import json
from datetime import datetime
import psutil

# اطلاعات Gitea شما
# نام کاربری: pythonkoft
# توکن شخصی: 2f135dee41b069f92413dd3f234e12cb8c20a96a
# آدرس ریپازیتوری: http://188.245.173.247:3080/pythonkoft/ea_shadow.git

REPO_DIR = "bot_ea_db"
GIT_REPO = "http://pythonkoft:2f135dee41b069f92413dd3f234e12cb8c20a96a@188.245.173.247:3080/pythonkoft/ea_shadow.git"
LOG_FILE = "log.json"
LAST_COMMIT_FILE = "last_commit.txt"
PROCESS_NAME = "main.bat"
PID_FILE = "main_pid.txt"

def write_log(event, message):
    log_entry = {
        "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "event": event,
        "message": message
    }
    if not os.path.exists(LOG_FILE):
        with open(LOG_FILE, "w", encoding="utf-8") as f:
            json.dump([log_entry], f, ensure_ascii=False, indent=2)
    else:
        with open(LOG_FILE, "r+", encoding="utf-8") as f:
            logs = json.load(f)
            logs.append(log_entry)
            f.seek(0)
            json.dump(logs, f, ensure_ascii=False, indent=2)

def kill_old_process():
    if not os.path.exists(PID_FILE):
        write_log("kill", "هیچ PIDی برای بستن وجود نداشت.")
        return

    try:
        with open(PID_FILE, "r") as f:
            pid = int(f.read().strip())

        parent = psutil.Process(pid)
        children = parent.children(recursive=True)

        for child in children:
            child.terminate()

        gone, still_alive = psutil.wait_procs(children, timeout=5)
        write_log("kill", f"{len(gone)} تا subprocess بسته شد. هنوز زنده‌ها: {len(still_alive)}")

        os.remove(PID_FILE)

    except (psutil.NoSuchProcess, ValueError):
        write_log("kill", "PID معتبر نبود یا پروسه پیدا نشد.")
    except Exception as e:
        write_log("kill", f"خطا در بستن subprocessها: {str(e)}")

def get_latest_commit():
    try:
        result = subprocess.run(
            ["git", "-C", REPO_DIR, "rev-parse", "HEAD"],
            capture_output=True,
            text=True
        )
        return result.stdout.strip()
    except Exception as e:
        write_log("error", f"get commit error: {str(e)}")
        return None

def save_last_commit(commit_hash):
    with open(LAST_COMMIT_FILE, "w") as f:
        f.write(commit_hash)

def load_last_commit():
    if os.path.exists(LAST_COMMIT_FILE):
        with open(LAST_COMMIT_FILE, "r") as f:
            return f.read().strip()
    return None

def clone_or_update_repo():
    if os.path.exists(REPO_DIR):
        try:
            result = subprocess.run(
                ["git", "-C", REPO_DIR, "pull"],
                capture_output=True,
                text=True
            )
            if result.returncode == 0:
                write_log("pull", "مخزن آپدیت شد.")
            else:
                write_log("error", f"خطا در git pull: {result.stderr}")
        except Exception as e:
            write_log("error", f"git pull failed: {str(e)}")
    else:
        result = subprocess.run(
            ["git", "clone", GIT_REPO, REPO_DIR],
            capture_output=True,
            text=True
        )
        if result.returncode == 0:
            write_log("clone", "مخزن کلون شد.")
        else:
            write_log("error", f"خطا در کلون کردن: {result.stderr}")

def run_main_bat():
    try:
        shutil.copy(os.path.join(os.getcwd(),'.env'),os.path.join(os.getcwd(),'bot_ea_db'))
        process = subprocess.Popen(
            ["cmd.exe", "/c", "main.bat"],
            cwd=REPO_DIR,
            creationflags=subprocess.CREATE_NEW_CONSOLE
        )

        with open(PID_FILE, "w") as f:
            f.write(str(process.pid))

        write_log("run", f"main.bat اجرا شد با PID={process.pid}.")
    except Exception as e:
        write_log("error", f"main.bat اجرا نشد: {str(e)}")

def main_loop():
    while True:
        print('1')
        clone_or_update_repo()
        print('2')
        latest_commit = get_latest_commit()
        print('3')
        saved_commit = load_last_commit()
        print('4')
        if latest_commit and latest_commit != saved_commit:
            print('5')
            write_log("update", f"کمیت جدید: {latest_commit}")
            print('6')
            kill_old_process()
            print('7')
            run_main_bat()
            print('8')
            save_last_commit(latest_commit)
        else:
            print(("check", "تغییری نبود."))
            write_log("check", "تغییری نبود.")
        print(60)
        time.sleep(60)

if __name__ == "__main__":
    main_loop()
