from crondb import CronDB
import time

db = CronDB()


def run_test():
    print("--- Starting CronDB Python SDK Test ---")

    # 1. Clean start
    db.query("DROP TABLE robots")

    # 2. CREATE TABLE (Manual Query)
    print("\n[1] Creating Table...")
    print(db.query("CREATE TABLE robots rid ApexInt name string status string 3"))

    # 3. INSERT via ORM
    print("\n[2] Inserting Data (Standard & Temporal)...")
    db.insert('robots', {'name': 'R2D2', 'status': 'Idle'})
    db.insert('robots', {
        'name': 'Terminator',
        'status': "('Searching' --> 'Found' @ 3 s)"
    })

    # 4. SELECT via ORM
    print("\n[3] Reading Data...")
    res = db.select('robots')
    print(f"Rows found: {res.get('rows_affected')}")
    for row in res.get('data', []):
        print(f" -> {row}")

    # 5. UPDATE via ORM
    print("\n[4] Updating R2D2...")
    db.update('robots', {'status': 'Beeping'}, "name = 'R2D2'")

    # 6. WAIT for FSM to fire
    print("\n[5] Waiting 4 seconds for Terminator to find target...")
    time.sleep(3)

    # 7. FINAL VERIFICATION
    print("\n[6] Final State Check...")
    final = db.select('robots', "name = 'Terminator'")
    print(f"Terminator Status: {final['data'][0]['status']}")

    print("\n--- Test Complete ---")


if __name__ == "__main__":
    run_test()