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

  // Desktop
  const ctx = await browser.newContext({
    viewport: { width: 1440, height: 900 },
    locale: 'ar',
  });
  const page = await ctx.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));
    }
  });
  await page.waitForTimeout(500);

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

  // 2. Roll the dice to see dots
  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, 'ludo-fix-02-rolled.png') });
    console.log('✓ ludo-fix-02-rolled.png');
  } else {
    console.log('⚠ No roll button found');
  }

  // 3. 4-player local game
  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, 'ludo-fix-03-4player.png') });
  console.log('✓ ludo-fix-03-4player.png');

  // Roll in 4p
  const rollBtn2 = await page.$('.ludo-roll-btn, #ludo-roll-btn');
  if (rollBtn2) {
    await rollBtn2.click();
    await page.waitForTimeout(1500);
    await page.screenshot({ path: path.join(DIR, 'ludo-fix-04-4player-rolled.png') });
    console.log('✓ ludo-fix-04-4player-rolled.png');
  }

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

  await mobile.goto(BASE + '/login', { waitUntil: 'networkidle', timeout: 15000 });
  await mobile.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 mobile.waitForTimeout(500);

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

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

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