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

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

(async () => {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({
    viewport: { width: 1440, height: 900 },
    locale: 'ar',
  });
  const page = await context.newPage();

  // Login
  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));
    }
  });

  // 1. Home page (streak strip + new button)
  console.log('1. Home page...');
  await page.goto(BASE + '/', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2000);
  await page.screenshot({ path: path.join(DIR, 'phase1-01-home-desktop.png'), fullPage: true });

  // 2. Games Hub
  console.log('2. Games Hub desktop...');
  await page.goto(BASE + '/games', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1500);
  await page.screenshot({ path: path.join(DIR, 'phase1-02-games-desktop.png'), fullPage: true });

  // 3. Chess lobby (breadcrumb)
  console.log('3. Chess lobby...');
  await page.goto(BASE + '/play', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1000);
  await page.screenshot({ path: path.join(DIR, 'phase1-03-chess-lobby.png'), fullPage: true });

  // 4. Ludo lobby (breadcrumb)
  console.log('4. Ludo lobby...');
  await page.goto(BASE + '/ludo', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1000);
  await page.screenshot({ path: path.join(DIR, 'phase1-04-ludo-lobby.png'), fullPage: true });

  // 5. Mobile Games Hub
  console.log('5. Mobile games hub...');
  await page.setViewportSize({ width: 390, height: 844 });
  await page.goto(BASE + '/games', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1500);
  await page.screenshot({ path: path.join(DIR, 'phase1-05-games-mobile.png'), fullPage: true });

  // 6. Mobile Home
  console.log('6. Mobile home...');
  await page.goto(BASE + '/', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2000);
  await page.screenshot({ path: path.join(DIR, 'phase1-06-home-mobile.png'), fullPage: true });

  // 7. Tablet Games Hub
  console.log('7. Tablet games...');
  await page.setViewportSize({ width: 768, height: 1024 });
  await page.goto(BASE + '/games', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1500);
  await page.screenshot({ path: path.join(DIR, 'phase1-07-games-tablet.png'), fullPage: true });

  await browser.close();
  console.log('Done!');
})();
