#!/usr/bin/env python3
"""Step 4: Delete duplicate contacts."""

import json
import subprocess
import sys
import time

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"
PLAN_FILE = "D:/ClaudeTools/temp/bardach_dedup_plan.json"
RESULTS_FILE = "D:/ClaudeTools/temp/bardach_dedup_delete_results.json"


def get_token():
    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)
    return data["access_token"]


def delete_contact(token, contact_id):
    url = f"https://graph.microsoft.com/v1.0/users/{USER}/contacts/{contact_id}"
    result = subprocess.run(
        ["curl", "-s", "-X", "DELETE", url,
         "-H", f"Authorization: Bearer {token}",
         "-w", "%{http_code}"],
        capture_output=True, text=True
    )
    # DELETE returns 204 No Content on success
    status_code = int(result.stdout.strip()[-3:]) if result.stdout.strip() else 0
    return status_code


def main():
    print("=" * 60)
    print("STEP 4: Delete duplicate contacts")
    print("=" * 60)

    with open(PLAN_FILE, "r", encoding="utf-8") as f:
        plan_data = json.load(f)

    # Collect all delete IDs
    all_deletes = []
    for entry in plan_data["plan"]:
        for did in entry["delete_ids"]:
            all_deletes.append({"id": did, "display_name": entry["display_name"]})

    print(f"[INFO] Total contacts to delete: {len(all_deletes)}")

    token = get_token()
    print("[OK] Token acquired")

    successes = 0
    failures = []
    start_time = time.time()

    for i, item in enumerate(all_deletes):
        status_code = delete_contact(token, item["id"])

        if status_code == 204 or status_code == 200:
            successes += 1
        else:
            failures.append({"display_name": item["display_name"], "id": item["id"], "status": status_code})

        # Progress every 100
        if (i + 1) % 100 == 0:
            elapsed = time.time() - start_time
            rate = (i + 1) / elapsed
            remaining = (len(all_deletes) - i - 1) / rate if rate > 0 else 0
            print(f"  Progress: {i + 1}/{len(all_deletes)} | Success: {successes} | Fail: {len(failures)} | {rate:.1f}/sec | ETA: {remaining:.0f}s")

        # Re-acquire token every 500 operations
        if (i + 1) % 500 == 0:
            print("  Re-acquiring token...")
            token = get_token()

        # Throttle: small pause every 50 to avoid 429
        if (i + 1) % 50 == 0:
            time.sleep(0.5)

    elapsed = time.time() - start_time

    results = {
        "total_attempted": len(all_deletes),
        "successes": successes,
        "failures": len(failures),
        "elapsed_seconds": round(elapsed, 1),
        "failure_details": failures
    }
    with open(RESULTS_FILE, "w", encoding="utf-8") as f:
        json.dump(results, f, indent=2, ensure_ascii=False)

    print(f"\n{'=' * 60}")
    print(f"DELETE RESULTS")
    print(f"{'=' * 60}")
    print(f"  Total attempted: {len(all_deletes)}")
    print(f"  Successes:       {successes}")
    print(f"  Failures:        {len(failures)}")
    print(f"  Elapsed:         {elapsed:.0f}s ({elapsed/60:.1f}m)")
    print(f"[OK] Results saved to {RESULTS_FILE}")


if __name__ == "__main__":
    main()
