#!/usr/bin/env python3
"""Step 1: Pull all Bardach Temp contacts and save as backup."""

import json
import subprocess
import sys
import os

TENANT_ID = "dd4a82e8-85a3-44ac-8800-07945ab4d95f"
CLIENT_ID = "fabb3421-8b34-484b-bc17-e46de9703418"
CLIENT_SECRET = "~QJ8Q~NyQSs4OcGqHZyPrA2CVnq9KBfKiimntbMO"
SCOPE = "https://graph.microsoft.com/.default"
USER = "barbara@bardach.net"
BACKUP_FILE = "D:/ClaudeTools/temp/bardach_temp_backup_prededup.json"

SELECT_FIELDS = "id,displayName,givenName,surname,emailAddresses,homePhones,businessPhones,companyName,jobTitle,personalNotes,homeAddress,businessAddress,otherAddress,birthday,nickName,categories,lastModifiedDateTime"


def get_token():
    """Get OAuth2 token via client credentials."""
    url = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token"
    result = subprocess.run(
        [
            "curl", "-s", "-X", "POST", url,
            "-H", "Content-Type: application/x-www-form-urlencoded",
            "-d", f"client_id={CLIENT_ID}&scope={SCOPE}&client_secret={CLIENT_SECRET}&grant_type=client_credentials"
        ],
        capture_output=True, text=True
    )
    data = json.loads(result.stdout)
    if "access_token" not in data:
        print(f"[ERROR] Failed to get token: {data}")
        sys.exit(1)
    print("[OK] Token acquired")
    return data["access_token"]


def graph_get(token, url):
    """Make a GET request to Graph API."""
    result = subprocess.run(
        [
            "curl", "-s", "-X", "GET", url,
            "-H", f"Authorization: Bearer {token}",
            "-H", "Content-Type: application/json"
        ],
        capture_output=True, text=True
    )
    return json.loads(result.stdout)


def find_temp_folder(token):
    """Find the Temp contact folder ID."""
    url = f"https://graph.microsoft.com/v1.0/users/{USER}/contactFolders"
    data = graph_get(token, url)
    if "value" not in data:
        print(f"[ERROR] Failed to get contact folders: {data}")
        sys.exit(1)
    for folder in data["value"]:
        print(f"  Found folder: {folder['displayName']} (id: {folder['id']})")
        if folder["displayName"].lower() == "temp":
            return folder["id"]
    # Check for child folders
    for folder in data["value"]:
        child_url = f"https://graph.microsoft.com/v1.0/users/{USER}/contactFolders/{folder['id']}/childFolders"
        child_data = graph_get(token, child_url)
        if "value" in child_data:
            for child in child_data["value"]:
                print(f"  Found subfolder: {folder['displayName']}/{child['displayName']} (id: {child['id']})")
                if child["displayName"].lower() == "temp":
                    return child["id"]
    print("[ERROR] Temp folder not found")
    sys.exit(1)


def pull_all_contacts(token, folder_id):
    """Pull all contacts from the Temp folder with pagination."""
    contacts = []
    url = f"https://graph.microsoft.com/v1.0/users/{USER}/contactFolders/{folder_id}/contacts?$top=100&$select={SELECT_FIELDS}"
    page = 1
    while url:
        print(f"  Fetching page {page}...")
        data = graph_get(token, url)
        if "value" not in data:
            print(f"[ERROR] Failed to get contacts: {data}")
            break
        contacts.extend(data["value"])
        print(f"  Got {len(data['value'])} contacts (total: {len(contacts)})")
        url = data.get("@odata.nextLink")
        page += 1
        # Re-acquire token every 50 pages to be safe
        if page % 50 == 0:
            print("  Re-acquiring token...")
            token = get_token()
    return contacts, token


def main():
    print("=" * 60)
    print("STEP 1: Pull all Temp contacts and save backup")
    print("=" * 60)

    token = get_token()

    print("\n[INFO] Finding Temp folder...")
    folder_id = find_temp_folder(token)
    print(f"[OK] Temp folder ID: {folder_id}")

    print("\n[INFO] Pulling all contacts...")
    contacts, token = pull_all_contacts(token, folder_id)

    print(f"\n[OK] Total contacts pulled: {len(contacts)}")

    # Save backup
    os.makedirs(os.path.dirname(BACKUP_FILE), exist_ok=True)
    with open(BACKUP_FILE, "w", encoding="utf-8") as f:
        json.dump({"total": len(contacts), "contacts": contacts}, f, indent=2, ensure_ascii=False)
    print(f"[OK] Backup saved to {BACKUP_FILE}")
    print(f"[OK] File size: {os.path.getsize(BACKUP_FILE) / 1024 / 1024:.1f} MB")


if __name__ == "__main__":
    main()
