#!/usr/bin/env python3
"""
Fetch data from Google Sheets master database
Export each tab as CSV and combine
"""

import requests
import csv
import json

# Master sheet ID
SHEET_ID = "1uVd-xZFF4TEQGqtvw9z6W8fffeaifPCoLsek83GmEoQ"

# Tab GIDs (need to find these from the sheet)
# Format: https://docs.google.com/spreadsheets/d/{SHEET_ID}/export?format=csv&gid={GID}

TABS = [
    "South Africa",
    "Australia", 
    "NZ",
    "Canada",
    "UK",
    "Other Countries"
]

def fetch_tab_as_csv(sheet_id: str, gid: str, tab_name: str) -> list:
    """Fetch a specific tab as CSV"""
    
    url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv&gid={gid}"
    
    print(f"Fetching {tab_name}...")
    
    try:
        response = requests.get(url)
        response.raise_for_status()
        
        # Parse CSV
        lines = response.text.strip().split('\n')
        reader = csv.DictReader(lines)
        
        data = list(reader)
        print(f"  ✓ {len(data)} rows")
        
        return data
    
    except Exception as e:
        print(f"  ✗ Error: {e}")
        return []

def main():
    print("Fetching Google Sheets tabs...")
    print("Note: Need to find GIDs for each tab")
    print("\nManual step required:")
    print("1. Open the sheet in browser")
    print("2. For each tab, look at URL: ...#gid=XXXXXX")
    print("3. Extract the GID numbers")
    print("\nSheet URL:")
    print(f"https://docs.google.com/spreadsheets/d/{SHEET_ID}/edit")

if __name__ == '__main__':
    main()
