#!/usr/bin/env node
import { chromium } from 'playwright';
import fs from 'fs';
import path from 'path';

(async () => {
	const browser = await chromium.launch({ headless: false });
	const context = await browser.newContext();
	const page = await context.newPage();

	console.log('🔹 Navigating to Instagram...');
	await page.goto('https://www.instagram.com/');
	console.log('⏳ Please log in manually. Waiting for "Home" icon...');

	try {
		await page.waitForSelector('svg[aria-label="Home"]', { timeout: 120000 });
		const secretsDir = path.resolve('../secrets');
		if (!fs.existsSync(secretsDir)) fs.mkdirSync(secretsDir);

		await context.storageState({ path: path.join(secretsDir, 'auth.json') });
		console.log('🎉 Session saved to secrets/auth.json');
	} catch (e) {
		console.error('❌ Timeout or error:', e);
	}
	await browser.close();
})();
