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

  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. Initial board state (all pieces in base)
  console.log('1. Board with pieces in base...');
  await page.goto(BASE + '/ludo-game?mode=bot&bots=3&difficulty=hard', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2000);
  await page.screenshot({ path: path.join(DIR, 'centered-01-base.png'), fullPage: true });

  // 2. Play until pieces move out
  console.log('2. Playing until pieces on track...');
  for (let i = 0; i < 50; i++) {
    const canRoll = await page.evaluate(() => {
      const btn = document.getElementById('ludo-roll-btn');
      return btn && !btn.hasAttribute('disabled');
    });
    if (canRoll) {
      await page.click('#ludo-roll-btn');
      await page.waitForTimeout(1200);
      await page.evaluate(() => {
        var hl = document.querySelectorAll('.ludo-piece--highlight');
        if (hl.length > 0) hl[0].click();
      });
    }
    await page.waitForTimeout(1000);
  }
  await page.screenshot({ path: path.join(DIR, 'centered-02-playing.png'), fullPage: true });

  // 3. Continue more
  for (let i = 0; i < 40; i++) {
    const canRoll = await page.evaluate(() => {
      const btn = document.getElementById('ludo-roll-btn');
      return btn && !btn.hasAttribute('disabled');
    });
    if (canRoll) {
      await page.click('#ludo-roll-btn');
      await page.waitForTimeout(1000);
      await page.evaluate(() => {
        var hl = document.querySelectorAll('.ludo-piece--highlight');
        if (hl.length > 0) hl[0].click();
      });
    }
    await page.waitForTimeout(900);
  }
  await page.screenshot({ path: path.join(DIR, 'centered-03-advanced.png'), fullPage: true });

  // 4. Local 4-player initial state
  console.log('3. Local 4-player initial...');
  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, 'centered-04-local4p.png'), fullPage: true });

  // 5. Mobile view
  console.log('4. Mobile view...');
  await page.setViewportSize({ width: 390, height: 844 });
  await page.goto(BASE + '/ludo-game?mode=bot&bots=1&difficulty=easy', { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(2000);
  // Play a few turns
  for (let i = 0; i < 30; i++) {
    const canRoll = await page.evaluate(() => {
      const btn = document.getElementById('ludo-roll-btn');
      return btn && !btn.hasAttribute('disabled');
    });
    if (canRoll) {
      await page.click('#ludo-roll-btn');
      await page.waitForTimeout(1200);
      await page.evaluate(() => {
        var hl = document.querySelectorAll('.ludo-piece--highlight');
        if (hl.length > 0) hl[0].click();
      });
    }
    await page.waitForTimeout(1000);
  }
  await page.screenshot({ path: path.join(DIR, 'centered-05-mobile.png'), fullPage: true });

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