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

test.describe('SAM Rough Mask Workflow', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('http://localhost:5001');
  });

  test('should show "Add Masked Element" button in review mode', async ({ page }) => {
    // Wait for Alpine.js to initialize
    await page.waitForSelector('button:has-text("Add Masked Element")');
    
    const addButton = page.getByRole('button', { name: 'Add Masked Element' });
    await expect(addButton).toBeVisible();
  });

  test('should enter add mode and show SAM section', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Click Add Masked Element
    await page.click('button:has-text("Add Masked Element")');
    
    // Should see Step 1 heading for rough mask
    const step1Heading = page.locator('h3:has-text("Step 1: Rough Mask (Optional)")');
    await expect(step1Heading).toBeVisible();
  });

  test('should show denoise slider when rough mask exists simulation', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // The denoise slider section should be in the DOM (though initially hidden)
    const denoiseSection = page.locator('input[type="range"][x-model*="denoiseStrength"]');
    await expect(denoiseSection).toHaveCount(1);
  });

  test('should have correct workflow steps visible', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Check all three step headings exist
    const step1 = page.locator('h3:has-text("Step 1: Rough Mask (Optional)")');
    const step2 = page.locator('h3:has-text("Step 2: Polygon (Optional)")');
    const step3 = page.locator('h3:has-text("Step 3: Generate Final Mask")');
    
    await expect(step1).toBeVisible();
    await expect(step2).toBeVisible();
    await expect(step3).toBeVisible();
  });

  test('should not have "Use as Mask" button for rough masks', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Verify the OLD "Use as Mask" button doesn't exist
    const useAsMaskButton = page.getByRole('button', { name: 'Use as Mask' });
    await expect(useAsMaskButton).not.toBeVisible();
  });

  test('should have "Discard & Start Over" button placeholder for rough mask', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // The discard button should exist in the DOM (within the conditional section)
    const sidebar = page.locator('.bg-gray-800').first();
    const hasDiscardButton = await page.$('button:has-text("Discard & Start Over")');
    // Button exists in template but hidden until rough mask is generated
  });

  test('should have Generate button instead of Extract', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Check for "Generate Mask" or "Generate Masks" button in Step 3
    const generateButton = page.locator('button:has-text("Generate Mask")');
    await expect(generateButton).toBeVisible();
  });

  test('should have denoise strength slider with proper range', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode  
    await page.click('button:has-text("Add Masked Element")');
    
    // Find the range input for denoise (it's in a conditional block)
    const denoiseSlider = page.locator('input[type="range"]');
    
    // Get all range inputs - there should be at least one (might be hidden initially)
    await expect(denoiseSlider).toHaveCount(1);
  });

  test('should have click to view full size text for rough mask thumbnail', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // The "click to view" overlay should exist in the template
    const clickableView = page.locator('text=Click to view full size');
    // Will be hidden until rough mask is generated, but exists in DOM structure
  });
});

test.describe('SAM Rough Mask UI Flow', () => {
  test('should allow entering SAM point mode', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Click Start button in SAM section
    const startButton = page.locator('button:has-text("Start")').first();
    await expect(startButton).toBeVisible();
  });

  test('should show include/exclude point counters', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Check for Include and Exclude labels in SAM section
    const includeLabel = page.locator('span:has-text("Include:")');
    const excludeLabel = page.locator('span:has-text("Exclude:")');
    
    // These might be hidden until points exist but should be in DOM
  });

  test('should have Generate Rough Mask button', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Find the Generate Rough Mask button
    const generateButton = page.getByRole('button', { name: 'Generate Rough Mask' });
    await expect(generateButton).toBeVisible();
  });

  test('should have Back to Review Mode button', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Check for back button at bottom of sidebar
    const backButton = page.getByRole('button', { name: 'Back to Review Mode' });
    await expect(backButton).toBeVisible();
  });

  test('should have use polygon hint checkbox', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Find the Use polygon hint checkbox
    const polygonCheckbox = page.locator('input[type="checkbox"]').filter({ hasText: 'Use polygon hint' }).first();
    await expect(polygonCheckbox).toBeVisible();
  });

  test('should have subject input field', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Find the subject input
    const subjectInput = page.locator('input[placeholder*="wooden door"]');
    await expect(subjectInput).toBeVisible();
  });

  test('should have mask count selector', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // Enter add mode
    await page.click('button:has-text("Add Masked Element")');
    
    // Find the count dropdown
    const countSelector = page.locator('select').filter({ hasText: 'mask' }).first();
    await expect(countSelector).toBeVisible();
  });

  test('should not show SAM overlay on canvas when rough mask exists', async ({ page }) => {
    await page.goto('http://localhost:5001');
    
    // The old SAM overlay image should NOT exist in the template anymore
    const oldOverlay = page.locator('img[alt="SAM mask preview"]');
    // This would be hidden anyway, but we want to ensure the element is removed from DOM structure
  });

  test.describe('Denoise Slider Configuration', () => {
    test('should display default denoise value of 80%', async ({ page }) => {
      await page.goto('http://localhost:5001');
      
      // Enter add mode
      await page.click('button:has-text("Add Masked Element")');
      
      // Verify Alpine.js initialized the denoiseStrength default value
      await page.evaluate(() => {
        const el = document.querySelector('[x-data="oraEditor()"]');
        const store = window.Alpine.$data(el);
        console.log('Denose strength:', store.denoiseStrength);
      });
    });

    test('should have "Using rough mask" indicator text', async ({ page }) => {
      await page.goto('http://localhost:5001');
      
      // Enter add mode
      await page.click('button:has-text("Add Masked Element")');
      
      // Check for the indicator text (hidden until rough mask exists)
      const indicator = page.locator('text=Using rough mask as starting point');
      // Will check it exists in DOM even when hidden
    });

    test('should have denoise helper text', async ({ page }) => {
      await page.goto('http://localhost:5001');
      
      // Enter add mode
      await page.click('button:has-text("Add Masked Element")');
      
      // Check for helper text explaining the denoise slider
      const helperText = page.locator('text=Lower = stick closer to rough mask');
      // Exists in DOM structure but initially hidden
    });
  });

  test.describe('UI Navigation', () => {
    test('should be able to clear SAM points', async ({ page }) => {
      await page.goto('http://localhost:5001');
      
      // Enter add mode
      await page.click('button:has-text("Add Masked Element")');
      
      // Clear button should exist in SAM section
      const clearButton = page.locator('button:has-text("Clear")').first();
      await expect(clearButton).toBeVisible();
    });

    test('should show point drawing instructions', async ({ page }) => {
      await page.goto('http://localhost:5001');
      
      // Enter add mode
      await page.click('button:has-text("Add Masked Element")');
      
      // SAM section should have instructions about mark points
      const instructions = page.locator('text=Click to mark include points');
      await expect(instructions).toBeVisible();
    });

    test('should have polygon helper text mentioning rough mask', async ({ page }) => {
      await page.goto('http://localhost:5001');
      
      // Enter add mode
      await page.click('button:has-text("Add Masked Element")');
      
      // Check for updated polygon description
      const polygonText = page.locator('text=Skip if rough mask is clear enough');
      await expect(polygonText).toBeVisible();
    });
  });
});
