/**
 * Token Debugging Commands
 * 
 * Run these in the Filevine page console (DevTools → Console)
 * to help debug token extraction
 */

// 1. Check if you're logged in (visual check)
console.log('=== Are you logged in? ===');
console.log('Can you see projects/documents in Filevine? If not, log in first.');

// 2. Check localStorage for tokens
console.log('\n=== Checking localStorage for tokens ===');
const localStorageKeys = Object.keys(localStorage);
const tokenKeys = localStorageKeys.filter(k => 
  k.toLowerCase().includes('token') || 
  k.toLowerCase().includes('auth') || 
  k.toLowerCase().includes('fv') ||
  k.toLowerCase().includes('oidc') ||
  k.toLowerCase().includes('access')
);

if (tokenKeys.length > 0) {
  console.log(`Found ${tokenKeys.length} potential token keys:`);
  tokenKeys.forEach(key => {
    const value = localStorage.getItem(key);
    // Show first 50 chars to avoid huge output
    const preview = value && value.length > 50 ? value.substring(0, 50) + '...' : value;
    console.log(`  ${key}:`, preview);
  });
} else {
  console.log('No token-related keys found in localStorage');
}

// 3. Check sessionStorage
console.log('\n=== Checking sessionStorage for tokens ===');
const sessionStorageKeys = Object.keys(sessionStorage);
const sessionTokenKeys = sessionStorageKeys.filter(k => 
  k.toLowerCase().includes('token') || 
  k.toLowerCase().includes('auth') || 
  k.toLowerCase().includes('fv') ||
  k.toLowerCase().includes('oidc') ||
  k.toLowerCase().includes('access')
);

if (sessionTokenKeys.length > 0) {
  console.log(`Found ${sessionTokenKeys.length} potential token keys:`);
  sessionTokenKeys.forEach(key => {
    const value = sessionStorage.getItem(key);
    const preview = value && value.length > 50 ? value.substring(0, 50) + '...' : value;
    console.log(`  ${key}:`, preview);
  });
} else {
  console.log('No token-related keys found in sessionStorage');
}

// 4. Check for JWT-like tokens (3 parts separated by dots)
console.log('\n=== Checking for JWT tokens ===');
const allStorageKeys = [...localStorageKeys, ...sessionStorageKeys];
let jwtFound = false;
allStorageKeys.forEach(key => {
  const value = localStorage.getItem(key) || sessionStorage.getItem(key);
  if (value && typeof value === 'string') {
    const parts = value.split('.');
    if (parts.length === 3 && parts.every(p => p.length > 0)) {
      console.log(`  Found JWT in: ${key}`);
      jwtFound = true;
    }
  }
});
if (!jwtFound) {
  console.log('No JWT tokens found');
}

// 5. Check page console for network interception message
console.log('\n=== Network Interception Check ===');
console.log('Look in the PAGE console (not extension console) for:');
console.log('  "[Filevine Plugin] Network interception active (page context)"');
console.log('If you see this, network interception is working.');

// 6. Check network tab for token requests
console.log('\n=== Network Tab Check ===');
console.log('1. Open DevTools → Network tab');
console.log('2. Filter by "token" or "identity"');
console.log('3. Look for requests to identity.filevine.com');
console.log('4. Check responses for "access_token"');

// 7. Test manual token extraction
console.log('\n=== Testing Manual Extraction ===');
console.log('Run this to test if we can find tokens:');
console.log(`
// Test extraction patterns
const testKeys = ['fv_access_token', 'access_token', 'fv_auth', 'auth', 'oidc.user', 'token'];
for (const key of testKeys) {
  const val = localStorage.getItem(key) || sessionStorage.getItem(key);
  if (val) {
    console.log('Found:', key, val.substring(0, 50));
  }
}
`);
