import { test, expect } from '@playwright/test';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));

async function loadCube(page: import('@playwright/test').Page) {
  await page.locator('#file-input').setInputFiles(resolve(__dirname, '../fixtures/cube.obj'));
  await page.waitForTimeout(3000);
}

test.describe('Desktop-Faithful Menu Structure', () => {
  test('Filters menu has all desktop categories', async ({ page }) => {
    await page.goto('/');
    await page.locator('canvas').first().waitFor({ state: 'visible', timeout: 10000 });
    await page.click('[data-testid="menu-Filters"]');

    // Desktop MeshLab filter categories
    for (const cat of [
      'Selection', 'Cleaning and Repairing', 'Remeshing',
      'Smoothing', 'Color Creation', 'Normals', 'Sampling', 'Texture',
    ]) {
      await expect(page.locator(`[data-testid="menu-bar"]`).locator(`text=${cat}`).first()).toBeVisible();
    }
  });

  test('previously disabled filter is now enabled', async ({ page }) => {
    await page.goto('/');
    await page.locator('canvas').first().waitFor({ state: 'visible', timeout: 10000 });

    await page.click('[data-testid="menu-Filters"]');
    await page.locator('[data-testid="menu-bar"]').locator('text=Sampling').click();

    // Clustered Vertex Sampling should now be enabled
    const item = page.locator('text=Clustered Vertex Sampling').first();
    await expect(item).toBeVisible();
    await expect(item.locator('..')).not.toHaveAttribute('data-disabled', 'true');
  });

  test('implemented filters are clickable', async ({ page }) => {
    await page.goto('/');
    await page.locator('canvas').first().waitFor({ state: 'visible', timeout: 10000 });
    await loadCube(page);

    await page.click('[data-testid="menu-Filters"]');
    await page.locator('[data-testid="menu-bar"]').locator('text=Smoothing').click();
    const laplacian = page.locator('text=Laplacian Smooth').first();
    await expect(laplacian).toBeVisible();
    // Not disabled
    const parent = laplacian.locator('..');
    const disabled = await parent.getAttribute('data-disabled');
    expect(disabled).not.toBe('true');
  });

  test('MeshLab icons copied to public/icons/', async ({ page }) => {
    // Check that key icons are accessible
    const response = await page.request.get('/icons/open.png');
    expect(response.status()).toBe(200);
    const response2 = await page.request.get('/icons/save.png');
    expect(response2.status()).toBe(200);
    const response3 = await page.request.get('/icons/layers.png');
    expect(response3.status()).toBe(200);
  });

  test('File menu matches desktop structure', async ({ page }) => {
    await page.goto('/');
    await page.click('[data-testid="menu-File"]');
    // Desktop items present
    await expect(page.locator('text=Import Mesh...').first()).toBeVisible();
    await expect(page.locator('text=Export Mesh As...').first()).toBeVisible();
    // Desktop-only items present but disabled
    await expect(page.locator('text=Import Raster...').first()).toBeVisible();
  });
});
