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);

  // ====== LUDO VERIFICATION ======
  console.log('\n=== LUDO ===');

  // Ludo initial (dice + pieces in base)
  await page.goto(BASE + '/ludo-game?mode=bot&bots=1&difficulty=easy', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2500);
  await page.screenshot({ path: path.join(DIR, 'verify-ludo-01-desktop.png') });
  console.log('✓ verify-ludo-01-desktop.png');

  // Roll dice
  const rollBtn = await page.$('.ludo-roll-btn, #ludo-roll-btn');
  if (rollBtn) {
    await rollBtn.click();
    await page.waitForTimeout(1500);
    await page.screenshot({ path: path.join(DIR, 'verify-ludo-02-rolled.png') });
    console.log('✓ verify-ludo-02-rolled.png');
  }

  // 4-player local
  await page.goto(BASE + '/ludo-game?mode=local&players=4', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2000);
  await page.screenshot({ path: path.join(DIR, 'verify-ludo-03-4player.png') });
  console.log('✓ verify-ludo-03-4player.png');

  // Mobile ludo
  await mobile.goto(BASE + '/ludo-game?mode=bot&bots=1&difficulty=easy', { waitUntil: 'networkidle', timeout: 15000 });
  await mobile.waitForTimeout(2500);
  await mobile.screenshot({ path: path.join(DIR, 'verify-ludo-04-mobile.png') });
  console.log('✓ verify-ludo-04-mobile.png');

  const mRoll = await mobile.$('.ludo-roll-btn, #ludo-roll-btn');
  if (mRoll) {
    await mRoll.click();
    await mobile.waitForTimeout(1500);
    await mobile.screenshot({ path: path.join(DIR, 'verify-ludo-05-mobile-rolled.png') });
    console.log('✓ verify-ludo-05-mobile-rolled.png');
  }

  // ====== DOMINO VERIFICATION ======
  console.log('\n=== DOMINO ===');

  // Domino lobby desktop
  await page.goto(BASE + '/domino', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1500);
  await page.screenshot({ path: path.join(DIR, 'verify-domino-01-lobby.png') });
  console.log('✓ verify-domino-01-lobby.png');

  // Domino 2P bot game
  await page.goto(BASE + '/domino-game?mode=2p&type=bot&players=2&difficulty=medium', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2500);
  await page.screenshot({ path: path.join(DIR, 'verify-domino-02-game-2p.png') });
  console.log('✓ verify-domino-02-game-2p.png');

  // Domino 4P teams bot
  await page.goto(BASE + '/domino-game?mode=4p_teams&type=bot&players=4&difficulty=hard', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2500);
  await page.screenshot({ path: path.join(DIR, 'verify-domino-03-game-4p.png') });
  console.log('✓ verify-domino-03-game-4p.png');

  // Domino local 2P
  await page.goto(BASE + '/domino-game?mode=2p&type=local&players=2', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2000);
  await page.screenshot({ path: path.join(DIR, 'verify-domino-04-local.png') });
  console.log('✓ verify-domino-04-local.png');

  // Domino mobile lobby
  await mobile.goto(BASE + '/domino', { waitUntil: 'networkidle', timeout: 15000 });
  await mobile.waitForTimeout(1500);
  await mobile.screenshot({ path: path.join(DIR, 'verify-domino-05-mobile-lobby.png') });
  console.log('✓ verify-domino-05-mobile-lobby.png');

  // Domino mobile game
  await mobile.goto(BASE + '/domino-game?mode=2p&type=bot&players=2&difficulty=easy', { waitUntil: 'networkidle', timeout: 15000 });
  await mobile.waitForTimeout(2500);
  await mobile.screenshot({ path: path.join(DIR, 'verify-domino-06-mobile-game.png') });
  console.log('✓ verify-domino-06-mobile-game.png');

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

  await browser.close();
  console.log('\n✅ All verification screenshots saved!');
})();
