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

test.describe('Find Event Creation Form', () => {
  test('should find the event creation form', async ({ page }) => {
    // Login as test_trainer
    await page.goto(`https://${STAGING_CONFIG.url}/wp-login.php`);
    await page.fill('#user_login', 'test_trainer');
    await page.fill('#user_pass', 'Test123!');
    await page.click('#wp-submit');
    
    // Wait for dashboard
    await page.waitForTimeout(3000);
    console.log('Logged in successfully');
    
    // Try different event creation URLs
    const urls = [
      `https://${STAGING_CONFIG.url}/events/community/add/`,
      `https://${STAGING_CONFIG.url}/events/community/add`,
      `https://${STAGING_CONFIG.url}/my-events/`,
      `https://${STAGING_CONFIG.url}/my-events`,
      `https://${STAGING_CONFIG.url}/hvac-dashboard/events/`,
      `https://${STAGING_CONFIG.url}/wp-admin/edit.php?post_type=tribe_events`
    ];
    
    for (const url of urls) {
      console.log(`Testing URL: ${url}`);
      
      try {
        await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 10000 });
        const pageTitle = await page.title();
        console.log(`Page title: ${pageTitle}`);
        
        // Look for event creation forms or links
        const hasEventForm = await page.locator('#post_title, input[name="title"], input[name="EventTitle"]').count() > 0;
        const hasSubmitButton = await page.locator('a:has-text("Submit Event"), button:has-text("Submit Event"), a:has-text("Add Event")').count() > 0;
        const hasMyEvents = await page.locator('a:has-text("My Events"), button:has-text("My Events")').count() > 0;
        
        console.log(`Has event form: ${hasEventForm}`);
        console.log(`Has submit button: ${hasSubmitButton}`);
        console.log(`Has my events: ${hasMyEvents}`);
        
        if (hasEventForm) {
          console.log('Found event creation form!');
          // Try to fill it
          const titleSelector = '#post_title, input[name="title"], input[name="EventTitle"]';
          const titleInput = page.locator(titleSelector).first();
          if (await titleInput.isVisible()) {
            await titleInput.fill(`Test Event ${Date.now()}`);
            console.log('Filled title successfully');
          }
        }
        
        if (hasSubmitButton) {
          console.log('Found submit event button');
          const submitLink = page.locator('a:has-text("Submit Event"), a:has-text("Add Event")').first();
          const href = await submitLink.getAttribute('href');
          console.log(`Submit link href: ${href}`);
        }
        
      } catch (error) {
        console.log(`Error accessing ${url}: ${error.message}`);
      }
      
      console.log('---');
    }
    
    // Also check the main dashboard page
    console.log('Checking main dashboard page...');
    await page.goto(`https://${STAGING_CONFIG.url}/hvac-dashboard/`);
    
    // Look for event-related links
    const eventLinks = await page.locator('a[href*="event"], a:has-text("Event")').all();
    console.log(`Found ${eventLinks.length} event-related links`);
    
    for (const link of eventLinks) {
      const text = await link.textContent();
      const href = await link.getAttribute('href');
      console.log(`Link: "${text}" -> ${href}`);
    }
  });
});