import { Page } from '@playwright/test';
import { DashboardPage } from '../pages/DashboardPage';
import { CreateEventPage } from '../pages/CreateEventPage';

interface EventData {
  title: string;
  description?: string;
  startDate?: string;
  endDate?: string;
  startTime?: string;
  endTime?: string;
  ticketType?: string;
  price?: string;
  venue?: string;
  organizer?: string;
}

/**
 * Helper function to create a test event
 * @param page Playwright Page object
 * @param eventData Event data to create
 * @returns Event ID of the created event
 */
export async function createTestEvent(page: Page, eventData: EventData): Promise<string> {
  // Navigate to dashboard
  const dashboardPage = new DashboardPage(page);
  await dashboardPage.navigate();
  
  // Click create event button
  await dashboardPage.clickCreateEvent();
  
  // Fill event form
  const createEventPage = new CreateEventPage(page);
  await createEventPage.fillEventTitle(eventData.title);
  
  if (eventData.description) {
    await createEventPage.fillEventDescription(eventData.description);
  }
  
  // Set dates and times if provided
  if (eventData.startDate) {
    await createEventPage.setStartDate(eventData.startDate);
  }
  
  if (eventData.endDate) {
    await createEventPage.setEndDate(eventData.endDate);
  }
  
  if (eventData.startTime) {
    await createEventPage.setStartTime(eventData.startTime);
  }
  
  if (eventData.endTime) {
    await createEventPage.setEndTime(eventData.endTime);
  }
  
  // Add ticket if price is provided
  if (eventData.ticketType && eventData.price) {
    await createEventPage.addTicket(eventData.ticketType, eventData.price);
  }
  
  // Set venue if provided
  if (eventData.venue) {
    await createEventPage.setVenue(eventData.venue);
  }
  
  // Set organizer if provided
  if (eventData.organizer) {
    await createEventPage.setOrganizer(eventData.organizer);
  }
  
  // Submit the form
  const eventId = await createEventPage.submitForm();
  
  return eventId;
}

/**
 * Helper function to get event ID from URL
 * @param url Event URL
 * @returns Event ID extracted from URL
 */
export function extractEventId(url: string): string {
  const match = url.match(/event_id=(\d+)/);
  if (match && match[1]) {
    return match[1];
  }
  return '';
}