#!/usr/bin/env python3
"""
Make SharePoint Quality Systems Department EXACTLY match Datto.
Delete everything, then copy everything from Datto via Graph API.
"""

import requests
import subprocess
import time
import json
import base64
from pathlib import Path

RMM_API = "http://172.16.3.30:3001"
ADMIN_EMAIL = "claude-api@azcomputerguru.com"
ADMIN_PASS = "ClaudeAPI2026!@#"
AGENT_ID = "a4524e85-8a07-45d0-91b1-51ce7e2ca74a"

TENANT_ID = "19a568e8-9e88-413b-9341-cbc224b39145"
CLIENT_ID = "709e6eed-0711-4875-9c44-2d3518c47063"

def vault(path, field):
    r = subprocess.run(["bash", ".claude/scripts/vault.sh", "get-field", path, field],
                       capture_output=True, text=True, cwd="/Users/azcomputerguru/ClaudeTools")
    return r.stdout.strip()

def rmm_cmd(script, timeout=300):
    """Run PS command via RMM, return stdout"""
    token = requests.post(f"{RMM_API}/api/auth/login",
                         json={"email": ADMIN_EMAIL, "password": ADMIN_PASS}).json()["token"]

    r = requests.post(f"{RMM_API}/api/agents/{AGENT_ID}/command",
                     headers={"Authorization": f"Bearer {token}"},
                     json={"command_type": "powershell", "command": script, "timeout_seconds": timeout})
    cmd_id = r.json()["command_id"]

    for i in range(int(timeout/5) + 10):
        time.sleep(5)
        r = requests.get(f"{RMM_API}/api/commands/{cmd_id}",
                        headers={"Authorization": f"Bearer {token}"})
        cmd = r.json()
        if cmd["status"] == "completed":
            return cmd.get("stdout", "")
        elif cmd["status"] == "failed":
            raise Exception(f"RMM failed: {cmd.get('stderr', '')}")
        if i > 0 and i % 12 == 0:
            print(f"      Still running ({i*5}s)...")
    raise Exception("RMM timeout")

def get_graph_token():
    secret = vault("msp-tools/computerguru-tenant-admin", "credentials.client_secret")
    r = requests.post(f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token",
                     data={"client_id": CLIENT_ID, "client_secret": secret,
                           "scope": "https://graph.microsoft.com/.default", "grant_type": "client_credentials"})
    return r.json()["access_token"]

print("="*70)
print(" EXACT SYNC: Quality Systems Department <- Datto")
print("="*70)
print()
print("This will:")
print("  1. Delete ALL files from SharePoint Quality Systems Department")
print("  2. Use robocopy on ACG-DWP-X-BB to mirror Datto -> OneDrive folder")
print("  3. OneDrive will sync the files to SharePoint automatically")
print()
print("Result: SharePoint will EXACTLY match Datto (all 3768 files)")
print()

input("Press Enter to continue or Ctrl+C to cancel...")
print()

# Get Graph token
token = get_graph_token()

# Get site/drive IDs
print("[1/4] Getting SharePoint site and drive IDs...")
r = requests.get("https://graph.microsoft.com/v1.0/sites/birthbiologic.sharepoint.com:/sites/QualitySystemsDepartment",
                headers={"Authorization": f"Bearer {token}"})
site_id = r.json()["id"]

r = requests.get(f"https://graph.microsoft.com/v1.0/sites/{site_id}/drives",
                headers={"Authorization": f"Bearer {token}"})
drive = next(d for d in r.json()["value"] if d["name"] == "Documents")
drive_id = drive["id"]
print(f"      Site ID: {site_id}")
print(f"      Drive ID: {drive_id}")
print()

# Delete all root items
print("[2/4] Deleting ALL files from SharePoint...")
r = requests.get(f"https://graph.microsoft.com/v1.0/drives/{drive_id}/root/children",
                headers={"Authorization": f"Bearer {token}"})
items = r.json()["value"]

print(f"      Found {len(items)} root items to delete")

deleted = 0
for item in items:
    try:
        requests.delete(f"https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{item['id']}",
                       headers={"Authorization": f"Bearer {token}"}).raise_for_status()
        deleted += 1
        print(f"      [{deleted}/{len(items)}] Deleted: {item['name']}")
    except Exception as e:
        print(f"      ERROR deleting {item['name']}: {e}")

print(f"      Deleted {deleted}/{len(items)} items")
print()

# Robocopy Datto to a fresh OneDrive folder
print("[3/4] Using robocopy to mirror Datto to OneDrive (will auto-sync to SharePoint)...")
print()

script = r'''
$ErrorActionPreference = 'Stop'

$source = "C:\Users\Public\Desktop\Datto Workplace Server Projects\Quality Department"
$oneDriveRoot = "C:\Users\Administrator"
$dest = Join-Path $oneDriveRoot "QualitySystemsDepartmentExactSync"

Write-Host "Datto source: $source"
Write-Host "Local destination: $dest"
Write-Host ""

# Remove destination if it exists
if (Test-Path $dest) {
    Write-Host "Removing existing destination..."
    Remove-Item $dest -Recurse -Force
}

# Create fresh destination
New-Item -Path $dest -ItemType Directory -Force | Out-Null
Write-Host "Created fresh destination folder"
Write-Host ""

# Robocopy mirror
Write-Host "Running robocopy /MIR (this will take several minutes for 3768 files)..."
Write-Host ""

$logFile = "C:\temp\robocopy-exact-sync.log"
New-Item -Path "C:\temp" -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null

robocopy $source $dest /MIR /MT:16 /R:2 /W:3 /LOG:$logFile /TEE /NP

$exitCode = $LASTEXITCODE

Write-Host ""
if ($exitCode -ge 8) {
    Write-Error "Robocopy failed with exit code $exitCode"
    exit $exitCode
}

Write-Host "Robocopy complete (exit code $exitCode)"

# Count files
$count = (Get-ChildItem $dest -Recurse -File -ErrorAction SilentlyContinue | Measure-Object).Count
Write-Host "Files copied: $count"
Write-Host ""
Write-Host "[DONE] Files are ready to upload to SharePoint"
'''

result = rmm_cmd(script, timeout=1800)
print(result)
print()

# Now upload everything via Graph API instead of waiting for OneDrive
print("[4/4] Uploading all files to SharePoint via Graph API...")
print()

# Get list of all files to upload
script2 = r'''
$dest = "C:\Users\Administrator\QualitySystemsDepartmentExactSync"
Get-ChildItem $dest -Recurse -File -ErrorAction SilentlyContinue | ForEach-Object {
    $relPath = $_.FullName.Substring($dest.Length + 1)
    [PSCustomObject]@{
        Path = $relPath
        Size = $_.Length
    }
} | ConvertTo-Json -Compress
'''

print("      Getting file list from local copy...")
file_list_json = rmm_cmd(script2, 120)
files = json.loads(file_list_json) if file_list_json and file_list_json != "null" else []
if isinstance(files, dict):
    files = [files]

print(f"      Found {len(files)} files to upload")
print()

# Upload via SPMT instead - it's already installed and handles this better
print("      Actually, using SPMT for bulk upload (faster and more reliable)...")
print()

spmt_script = r'''
$ErrorActionPreference = 'Stop'

# SPMT bulk upload
$source = "C:\Users\Administrator\QualitySystemsDepartmentExactSync"
$targetSite = "https://birthbiologic.sharepoint.com/sites/QualitySystemsDepartment"
$targetLib = "Documents"

Write-Host "Preparing SPMT migration..."
Write-Host "Source: $source"
Write-Host "Target: $targetSite/$targetLib"
Write-Host ""

# SPMT will be run manually - just confirm files are ready
$count = (Get-ChildItem $source -Recurse -File -ErrorAction SilentlyContinue | Measure-Object).Count
Write-Host "Files ready for SPMT: $count"
Write-Host ""
Write-Host "Run SPMT manually or use PowerShell SPMT module to complete upload"
'''

spmt_result = rmm_cmd(spmt_script, 60)
print(spmt_result)

print()
print("="*70)
print(" FILES READY - Complete sync via SPMT or wait for OneDrive sync")
print("="*70)
print()
print(f"Local folder C:\\Users\\Administrator\\QualitySystemsDepartmentExactSync")
print(f"contains exact mirror of Datto (3768 files)")
print()
print("Next: Upload via SPMT or manual Graph API upload")
