const { test, expect } = require('@playwright/test');
const fs = require('fs');
const path = require('path');

// Helper: Ensure folder exists
function ensureFolderExists(folderPath) {
  if (!fs.existsSync(folderPath)) {
    fs.mkdirSync(folderPath, { recursive: true });
  }
}

// Function to take screenshots at different viewport sizes
async function takeScreenshotsAtViewports(page, folderName, screenshotPrefix) {
  const viewports = [
    { width: 1230, height: 629, name: 'moniter-spec' },
    { width: 1280, height: 800, name: 'lap-spec' },
    { width: 1920, height: 1080, name: 'TV-spec' },
  ];

  const folderPath = path.join(__dirname, '..', 'test-results', folderName);
  ensureFolderExists(folderPath);

  for (const { width, height, name } of viewports) {
    await page.setViewportSize({ width, height });
    await page.waitForTimeout(500); // Allow viewports to settle slightly
    const screenshotPath = path.join(folderPath, `${screenshotPrefix}-${name}.png`);
    await page.screenshot({ path: screenshotPath, fullPage: true });
    console.log(`Screenshot taken for ${name} resolution in folder ${folderName}`);
  }
}

// Utility: Login to the app
async function login(page) {
  const url = 'http://185.100.212.76:8200';
  await page.goto(url, { waitUntil: 'networkidle' });

  await page.getByPlaceholder('Email').fill('ramkumar@tester.local');
  await page.getByPlaceholder('Password').fill('123456');

  await page.getByRole('button', { name: 'Continue', exact: true }).click();
  await page.waitForLoadState('networkidle'); // Wait after login
}

test('Login Page Screenshots', async ({ page }) => {
  test.setTimeout(60000);

  try {
    await page.goto('http://185.100.212.76:8200', { waitUntil: 'networkidle' });
    await page.waitForSelector('[placeholder="Email"]', { timeout: 10000 });
    await takeScreenshotsAtViewports(page, 'login-screenshots/login-page-loaded', 'login-page-loaded');

    console.log('Filling Email and Password fields...');
    await page.getByPlaceholder('Email').fill('ramkumar@tester.local');
    await page.getByPlaceholder('Password').fill('123456');
    await takeScreenshotsAtViewports(page, 'login-screenshots/fields-filled', 'fields-filled');

    console.log('Clicking the Continue button...');
    await page.getByRole('button', { name: 'Continue', exact: true }).click();
    await page.waitForLoadState('networkidle');
    await takeScreenshotsAtViewports(page, 'login-screenshots/after-login', 'after-login');
  } catch (error) {
    console.error('An error occurred during login test:', error.message);
    throw error;
  }
});

test('Simulation Page Screenshots', async ({ page }) => {
  test.setTimeout(120000); // Increase timeout for reliability

  await login(page);

  // Navigate to Simulation section
  await page.getByText('Simulation', { exact: true }).click();
  await page.waitForLoadState('networkidle');
  await takeScreenshotsAtViewports(page, 'simulation-screenshots/simulation-page-loaded', 'simulation-page-loaded');
});

test('Visualization Page Screenshots', async ({ page }) => {
  test.setTimeout(120000);

  await login(page);

  // Navigate to Visualization section
  await page.getByText('Visualization', { exact: true }).click();
  await page.waitForLoadState('networkidle');

  await takeScreenshotsAtViewports(page, 'Visualization-screenshots/Visualization-page1-loaded', 'Visualization1-page-loaded');

  await page.locator('.toggle-header-item:has-text("3D")').click();
  await takeScreenshotsAtViewports(page, 'Visualization-screenshots/Visualization-page1-loaded', 'Visualization1-page-loaded-3D');

  await page.locator('.toggle-header-item:has-text("Floating")').click();
  await takeScreenshotsAtViewports(page, 'Visualization-screenshots/Visualization-page1-loaded', 'Visualization1-page-loaded-Floating');

  await page.locator('.toggle-header-item:has-text("Templates")').click();
  await takeScreenshotsAtViewports(page, 'Visualization-screenshots/Visualization-page2-loaded', 'Visualization2-page-loaded');

  
});

test('Market Place Page Screenshots', async ({ page }) => {
  test.setTimeout(120000);

  await login(page);

  // Navigate to Market Place section
  await page.getByText('Market Place', { exact: true }).click();
  await page.waitForLoadState('networkidle');
  await takeScreenshotsAtViewports(page, 'market-place-screenshots/Market-Place-page-loaded', 'Market-Place-page-loaded');
});