/**
 * Firefox Cookie Converter - Convert Firefox cookie export to Puppeteer format
 *
 * USAGE:
 * 1. Firefox → F12 → Storage → Cookies → minsu.xiaozhu.com
 * 2. Copy all cookies (Ctrl+A, Ctrl+C in the cookie list)
 * 3. Paste the JSON export here in the cookiesRaw variable
 * 4. Run: node firefox_cookie_converter.js
 */

// PASTE YOUR FIREFOX COOKIES HERE (as JSON array or as tab-separated values)
// Example format from Firefox:
// [{"name":"sessionid","value":"abc123","domain":".xiaozhu.com",...}, ...]

const fs = require('fs');

// Method 1: If you have JSON export from Firefox extension
function convertFromJSON(firefoxCookies) {
  return firefoxCookies.map(cookie => ({
    name: cookie.name,
    value: cookie.value,
    domain: cookie.domain || cookie.host,
    path: cookie.path || '/',
    expires: cookie.expirationDate || cookie.expiry || -1,
    httpOnly: cookie.httpOnly || false,
    secure: cookie.secure || false,
    sameSite: cookie.sameSite || 'Lax'
  }));
}

// Method 2: Parse from Firefox DevTools copy (tab-separated)
function convertFromDevTools(rawText) {
  const lines = rawText.trim().split('\n');
  const cookies = [];

  for (const line of lines) {
    // Firefox DevTools format: Name\tValue\tDomain\tPath\tExpires\tHttpOnly\tSecure
    const parts = line.split('\t');
    if (parts.length >= 2) {
      cookies.push({
        name: parts[0],
        value: parts[1],
        domain: parts[2] || '.xiaozhu.com',
        path: parts[3] || '/',
        expires: parts[4] ? new Date(parts[4]).getTime() / 1000 : -1,
        httpOnly: parts[5] === 'true' || parts[5] === '✓',
        secure: parts[6] === 'true' || parts[6] === '✓',
        sameSite: 'Lax'
      });
    }
  }

  return cookies;
}

// ============================================
// PASTE YOUR COOKIES BELOW THIS LINE
// ============================================

// Option A: If you have JSON from Firefox Cookie Editor extension
const cookiesJSON = null; // Replace with your JSON array

// Option B: If you copied from DevTools Storage tab (paste between ` `)
const cookiesDevTools = `
PASTE_HERE_YOUR_COOKIES_FROM_FIREFOX_DEVTOOLS
`;

// ============================================

let puppeteerCookies;

if (cookiesJSON) {
  puppeteerCookies = convertFromJSON(cookiesJSON);
} else if (cookiesDevTools && !cookiesDevTools.includes('PASTE_HERE')) {
  puppeteerCookies = convertFromDevTools(cookiesDevTools);
} else {
  console.log('⚠️  NO COOKIES FOUND!');
  console.log('\nHow to extract cookies from Firefox:\n');
  console.log('METHOD 1 (Easiest):');
  console.log('1. Install "Cookie-Editor" Firefox extension');
  console.log('2. Go to https://minsu.xiaozhu.com/');
  console.log('3. Click Cookie-Editor icon → Export → Copy all as JSON');
  console.log('4. Paste into cookiesJSON variable above\n');
  console.log('METHOD 2 (Manual):');
  console.log('1. Firefox → F12 → Storage tab');
  console.log('2. Expand "Cookies" → Click "https://minsu.xiaozhu.com"');
  console.log('3. Select all cookies (Ctrl+A)');
  console.log('4. Right click → Copy');
  console.log('5. Paste into cookiesDevTools variable above\n');
  process.exit(1);
}

// Save to file
const outputPath = './xiaozhu_cookies.json';
fs.writeFileSync(outputPath, JSON.stringify(puppeteerCookies, null, 2));

console.log('✅ Cookies converted successfully!');
console.log(`📁 Saved to: ${outputPath}`);
console.log(`🍪 Total cookies: ${puppeteerCookies.length}`);
console.log('\nCookie details:');
puppeteerCookies.forEach(c => {
  console.log(`   - ${c.name}: ${c.value.substring(0, 20)}...`);
});
console.log('\n🚀 You can now run: node xiaozhu_minsu_scraper.js');
