import { Page } from '@playwright/test';
import { BasePage } from './BasePage';

export class EventSummaryPage extends BasePage {
    private readonly editEventButton = 'a:has-text("Edit Event")';
    private readonly emailAttendeesButton = 'a:has-text("Email Attendees")';
    private readonly returnToDashboardButton = 'a:has-text("Return to Dashboard")';
    private readonly eventDetails = '.event-details';
    private readonly transactionsTable = '.transactions-table';
    private readonly eventTitle = '.event-title';
    private readonly eventDate = '.event-date';
    private readonly eventLocation = '.event-location';
    private readonly eventOrganizer = '.event-organizer';
    private readonly ticketInfo = '.ticket-info';
    private readonly eventDescription = '.event-description';

    constructor(page: Page) {
        super(page);
    }

    async navigateToEventSummary(eventId: string): Promise<void> {
        await this.navigate(`/event-summary/?event_id=${eventId}`);
    }

    async clickEditEvent(): Promise<void> {
        await this.click(this.editEventButton);
        await this.waitForNavigation();
    }

    async clickEmailAttendees(): Promise<void> {
        await this.click(this.emailAttendeesButton);
        await this.waitForNavigation();
    }

    async returnToDashboard(): Promise<void> {
        await this.click(this.returnToDashboardButton);
        await this.waitForNavigation();
    }

    async getEventDetails(): Promise<{
        title: string;
        date: string;
        location: string;
        organizer: string;
        ticketInfo: string;
        description: string;
    }> {
        return {
            title: await this.getText(this.eventTitle),
            date: await this.getText(this.eventDate),
            location: await this.getText(this.eventLocation),
            organizer: await this.getText(this.eventOrganizer),
            ticketInfo: await this.getText(this.ticketInfo),
            description: await this.getText(this.eventDescription)
        };
    }

    async isTransactionsTableVisible(): Promise<boolean> {
        return await this.isVisible(this.transactionsTable);
    }

    async getTransactionData(index: number): Promise<{
        purchaserName: string;
        organization: string;
        purchaseDate: string;
        ticketCount: string;
        revenue: string;
    }> {
        const row = await this.page.locator(`${this.transactionsTable} tbody tr`).nth(index);
        
        return {
            purchaserName: await row.locator('td:nth-child(1)').textContent() || '',
            organization: await row.locator('td:nth-child(2)').textContent() || '',
            purchaseDate: await row.locator('td:nth-child(3)').textContent() || '',
            ticketCount: await row.locator('td:nth-child(4)').textContent() || '',
            revenue: await row.locator('td:nth-child(5)').textContent() || ''
        };
    }

    async getTransactionCount(): Promise<number> {
        return await this.page.locator(`${this.transactionsTable} tbody tr`).count();
    }

    async clickPurchaserName(name: string): Promise<void> {
        await this.page.click(`${this.transactionsTable} a:has-text("${name}")`);
        await this.waitForNavigation();
    }
}