import { expect, test } from '@playwright/test';

const IMAGE = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M/wHwAF/gL+Xw0YAAAAAElFTkSuQmCC';

for (const width of [1280, 390, 320]) {
  test(`social formats stay readable with Vela theme at ${width}px`, async ({ page }) => {
    await page.setViewportSize({ width, height: 960 });
    await page.addInitScript((image) => {
      localStorage.setItem('aisthetix_tryon_consent', '1');
      localStorage.setItem('aisthetix-avatar', JSON.stringify({ imageData: `data:image/png;base64,${image}`, createdAt: new Date().toISOString() }));
    }, IMAGE);
    await page.route('**/garment.png', route => route.fulfill({ contentType: 'image/png', body: Buffer.from(IMAGE, 'base64') }));
    await page.route('**/api/storefront/tryon', route => route.fulfill({ contentType: 'application/json', body: JSON.stringify({ image: IMAGE, cached: false }) }));
    await page.goto('/widget-src/e2e/fixtures/product.html?demo=1');
    // Actual Vela customizer declarations, loaded after the plugin stylesheet.
    await page.addStyleTag({ content: `button { background:#1C2B45!important; color:#FBFAF7!important; border:1px solid #1C2B45!important; border-radius:0!important; text-transform:uppercase; letter-spacing:.13em; font-size:.72rem; font-weight:500; padding:.95em 1.9em; }` });
    await page.getByRole('button', { name: 'Try it on', exact: true }).click();
    await page.getByRole('button', { name: 'Share your look', exact: true }).click();
    // Full result and narrower saved-look row body.
    for (const gallery of [false, true]) {
      if (gallery) {
        await page.getByRole('button', { name: /Your try-ons/ }).click();
        await page.getByRole('button', { name: 'Share', exact: true }).first().click();
      }
      const channels = page.locator('.aisthetix-share__channel');
      await expect(channels).toHaveCount(5);
      await expect(page.getByRole('button', { name: 'Download image', exact: true })).toBeEnabled();
      const menu = page.locator('.aisthetix-share__menu');
      await expect(menu.getByRole('link')).toHaveCount(0);
      await expect(menu.getByRole('button', { name: 'Copy product link' })).toHaveCount(0);
      const downloaded = page.waitForEvent('download');
      await page.getByRole('button', { name: 'Download image', exact: true }).click();
      expect((await downloaded).suggestedFilename()).toBe('aisthetix-try-on-story.png');
      await expect(menu.locator('.aisthetix-share__status')).toHaveCount(0);
      const measurements = await channels.evaluateAll(buttons => buttons.map(button => {
        const label = button.querySelector('.aisthetix-share__badge') as HTMLElement;
        const style = getComputedStyle(button);
        const labelStyle = getComputedStyle(label);
        const rect = button.getBoundingClientRect();
        const labelRect = label.getBoundingClientRect();
        return { transform: style.textTransform, spacing: style.letterSpacing, background: style.backgroundColor,
          fontSize: parseFloat(labelStyle.fontSize), fits: label.scrollWidth <= label.clientWidth && labelRect.left >= rect.left && labelRect.right <= rect.right,
          inViewport: rect.left >= 0 && rect.right <= innerWidth, height: rect.height };
      }));
      for (const measurement of measurements) {
        expect(measurement.transform).toBe('none');
        expect(measurement.spacing).toBe('normal');
        expect(measurement.background).toBe('rgba(0, 0, 0, 0)');
        expect(measurement.fontSize).toBeGreaterThanOrEqual(12);
        expect(measurement.fits).toBe(true);
        expect(measurement.inViewport).toBe(true);
        expect(measurement.height).toBeGreaterThanOrEqual(44);
      }
    }
  });
}
