import requests
import json
import time

def fetch_all_boi_data():
    url = "https://stemplus.or.th/api/boi"
    all_courses = []
    
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
    }

    print("Starting data collection for pages 1 to 18...")

    for page in range(1, 19):  # 1 until 18 (inclusive)
        payload = {
            'p': str(page),
            'pt': 'new',
            'ius[]': '9',
            'w': ''
        }
        
        try:
            print(f"Fetching page {page}...", end="\r")
            response = requests.post(url, data=payload, headers=headers)
            response.raise_for_status()
            
            result = response.json()
            
            if result.get("success"):
                courses = result.get("data", {}).get("data", [])
                
                for course in courses:
                    filtered_course = {
                        "course_name": course.get("course_name"),
                        "organization_name": course.get("organization_name"),
                        "count_view": course.get("count_view"),
                        "count_register": course.get("count_register"),
                        "uuid": course.get("uuid")
                    }
                    all_courses.append(filtered_course)
            else:
                print(f"\nAPI Error on page {page}: {result.get('message')}")
                
            # Small delay to be polite to the server
            time.sleep(0.5)
            
        except requests.exceptions.RequestException as e:
            print(f"\nRequest failed on page {page}: {e}")
            break

    # Write the big JSON array to courses.json
    output_file = "courses.json"
    timestamp_file = "timestamp.json"
    try:
        with open(output_file, "w", encoding="utf-8") as f:
            json.dump(all_courses, f, indent=4, ensure_ascii=False)
        print(f"\nDone! Collected {len(all_courses)} courses and saved to {output_file}")
        
        # Save timestamp
        from datetime import datetime
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        with open(timestamp_file, "w", encoding="utf-8") as f:
            json.dump({"last_update": current_time}, f, indent=4)
        print(f"Timestamp updated in {timestamp_file}: {current_time}")
        
    except Exception as e:
        print(f"\nError saving data: {e}")

if __name__ == "__main__":
    fetch_all_boi_data()
