import os
import sys
import subprocess
import time

def run_script(script_path, args=None):
    cmd = [sys.executable, script_path]
    if args:
        cmd.extend(args)
    
    print(f"\n[REFRESH] Running: {' '.join(cmd)}")
    start_time = time.time()
    
    result = subprocess.run(cmd)
    
    elapsed = time.time() - start_time
    if result.returncode != 0:
        print(f"[REFRESH] Error running {script_path}. Exit code: {result.returncode}")
        sys.exit(result.returncode)
    else:
        print(f"[REFRESH] Finished {script_path} in {elapsed:.2f}s")

def main():
    base_dir = os.path.dirname(os.path.abspath(__file__))
    project_root = os.path.dirname(base_dir)
    
    print("="*50)
    print("STARTING FULL DATABASE REFRESH")
    print("="*50)
    
    # 1. L1A --force (Re-ingest all raw data)
    l1a_script = os.path.join(base_dir, 'L1A.py')
    run_script(l1a_script, ['--force'])
    
    # 2. L2 Builder (Rebuild Fact Tables with fixed K/D logic)
    l2_script = os.path.join(base_dir, 'L2_Builder.py')
    run_script(l2_script)
    
    # 3. L3 Builder (Rebuild Feature Store)
    l3_script = os.path.join(base_dir, 'L3_Builder.py')
    run_script(l3_script)
    
    print("="*50)
    print("DATABASE REFRESH COMPLETED SUCCESSFULLY")
    print("="*50)

if __name__ == "__main__":
    main()
