const { chromium } = require('playwright');
const path = require('path');

const BASE = 'https://el3ab-player.caprover.al-arcade.com';
const DIR = __dirname;

async function login(page) {
  await page.goto(BASE + '/login', { waitUntil: 'networkidle', timeout: 15000 });
  await page.evaluate(async () => {
    const res = await fetch('https://safe-supabase-kong.caprover.al-arcade.com/auth/v1/token?grant_type=password', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'apikey': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlzcyI6InN1cGFiYXNlIiwiaWF0IjoxNzM1Njg5NjAwLCJleHAiOjE4OTM0NTYwMDB9.31PF6PvP-pSrvRuQwLFptQoejR0W1A7o53lZhEbnz84'
      },
      body: JSON.stringify({ email: 'uitest@el3ab.com', password: 'UiTest2026!' })
    });
    const data = await res.json();
    if (data.access_token) {
      localStorage.setItem('el3ab_token', data.access_token);
      localStorage.setItem('el3ab_user', JSON.stringify(data.user));
    }
  });
  await page.waitForTimeout(500);
}

(async () => {
  const browser = await chromium.launch({ headless: true });

  // Desktop
  const ctx = await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: 'ar' });
  const page = await ctx.newPage();
  await login(page);

  // Mobile
  const mCtx = await browser.newContext({ viewport: { width: 390, height: 844 }, locale: 'ar', isMobile: true });
  const mobile = await mCtx.newPage();
  await login(mobile);

  console.log('\n=== BACKGAMMON ===');

  // 1. Lobby
  await page.goto(BASE + '/backgammon', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1500);
  await page.screenshot({ path: path.join(DIR, 'backgammon-01-lobby.png'), fullPage: true });
  console.log('✓ backgammon-01-lobby.png');

  // 2. Bot game (medium)
  await page.goto(BASE + '/backgammon-game?mode=bot&difficulty=medium', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(3000);
  await page.screenshot({ path: path.join(DIR, 'backgammon-02-game-bot.png') });
  console.log('✓ backgammon-02-game-bot.png');

  // 3. Local game
  await page.goto(BASE + '/backgammon-game?mode=local', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2500);
  await page.screenshot({ path: path.join(DIR, 'backgammon-03-game-local.png') });
  console.log('✓ backgammon-03-game-local.png');

  // 4. Mobile lobby
  await mobile.goto(BASE + '/backgammon', { waitUntil: 'networkidle', timeout: 15000 });
  await mobile.waitForTimeout(1500);
  await mobile.screenshot({ path: path.join(DIR, 'backgammon-04-mobile-lobby.png') });
  console.log('✓ backgammon-04-mobile-lobby.png');

  // 5. Mobile game
  await mobile.goto(BASE + '/backgammon-game?mode=bot&difficulty=easy', { waitUntil: 'networkidle', timeout: 15000 });
  await mobile.waitForTimeout(3000);
  await mobile.screenshot({ path: path.join(DIR, 'backgammon-05-mobile-game.png') });
  console.log('✓ backgammon-05-mobile-game.png');

  // 6. Games hub (verify backgammon shows)
  await page.goto(BASE + '/games', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1500);
  await page.screenshot({ path: path.join(DIR, 'backgammon-06-games-hub.png') });
  console.log('✓ backgammon-06-games-hub.png');

  // 7. Check for JS errors
  const errors = [];
  page.on('pageerror', err => errors.push(err.message));
  await page.goto(BASE + '/backgammon-game?mode=bot&difficulty=hard', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(3000);
  await page.screenshot({ path: path.join(DIR, 'backgammon-07-hard-bot.png') });
  console.log('✓ backgammon-07-hard-bot.png');
  if (errors.length > 0) {
    console.log('⚠ JS Errors:', errors.join('; '));
  }

  await browser.close();
  console.log('\n✅ Backgammon screenshots complete!');
})();
