import subprocess
import os
import sys

# --- CONFIGURATION ---
# Change these paths to match your actual folders
SCRAPER_SCRIPT = "scrapper2.py"
JOB_GEN_SCRIPT = "j3.py"
HOME_GEN_SCRIPT = "home2.py"
SITEMAP_SCRIPT = "sitemap-003.py"

NDJSON_DATA = "jobs.ndjson"
PUBLIC_HTML_DIR = "./public_html"
JOB_SUBDIR = "./public_html/job"

def run_command(cmd, description):
    print(f"--- [STEP] {description} ---")
    try:
        # We use sys.executable to ensure we use the same Python version
        result = subprocess.run(f"{sys.executable} {cmd}", shell=True, check=True)
        print(f"--- [DONE] {description} successful. ---\n")
    except subprocess.CalledProcessError as e:
        print(f"!!! [ERROR] {description} failed. Stopping pipeline. !!!")
        sys.exit(1)

def main():
    print("🚀 Starting Daily Site Update Pipeline...\n")

    # 1. Run Scraper to get fresh data (optional: comment out if you already have data)
    run_command(SCRAPER_SCRIPT, "Scraping fresh job data")

    # 2. Run Job Page Generator (j3.py)
    # This creates the /job/ folder and HTML files
    run_command(f"{JOB_GEN_SCRIPT} {NDJSON_DATA} {PUBLIC_HTML_DIR}", "Generating Job Pages and Schema")

    # 3. Run Homepage Generator (home2.py)
    # This reads the /job/ folder and updates index.html
    run_command(f"{HOME_GEN_SCRIPT} --jobs_dir {JOB_SUBDIR} --output_dir {PUBLIC_HTML_DIR}", "Updating Homepage Index")

    # 4. Run Sitemap Generator (sitemap-003.py)
    # This prepares the XML for Google
    run_command(SITEMAP_SCRIPT, "Building SEO Sitemaps")

    print("✨ ALL STEPS COMPLETED SUCCESSFULLY!")
    print(f"Your site is ready in: {os.path.abspath(PUBLIC_HTML_DIR)}")

if __name__ == "__main__":
    main()