/**
 * @fileoverview E2E Tests for Order Summary page.
 * This test suite verifies that trainers can view order details for their events.
 */

import { test, expect } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
import { DashboardPage } from './pages/DashboardPage';

// Test data
const TEST_USER = {
    username: process.env.TEST_TRAINER_USERNAME || 'test_trainer',
    password: process.env.TEST_TRAINER_PASSWORD || 'Test_password123'
};

test.describe('Order Summary Page @order-summary', () => {
    test.beforeEach(async ({ page }) => {
        // Login before each test
        const loginPage = new LoginPage(page);
        await loginPage.goto();
        await loginPage.login(TEST_USER.username, TEST_USER.password);
        
        // Verify we're logged in by checking if we're on the dashboard
        const dashboardPage = new DashboardPage(page);
        await expect(page).toHaveURL(/.*hvac-dashboard.*/);
        await expect(dashboardPage.welcomeMessage).toBeVisible();
    });

    test('should navigate from dashboard to event summary to order summary', async ({ page }) => {
        // Navigate to dashboard
        await page.goto('/hvac-dashboard/');
        await expect(page).toHaveURL(/.*hvac-dashboard.*/);
        
        // Find an event with tickets and navigate to its event summary
        // This depends on having test events with ticket sales
        const eventLinks = await page.$$('a[href*="event-summary"]');
        if (eventLinks.length === 0) {
            test.skip('No events with ticket sales found');
        }
        
        // Click the first event link
        await eventLinks[0].click();
        await expect(page).toHaveURL(/.*event-summary.*/);
        await expect(page.locator('h1')).toContainText('Summary');
        
        // Look for order links in the transactions table
        const orderLinks = await page.$$('a[href*="order-summary"]');
        if (orderLinks.length === 0) {
            test.skip('No orders found for this event');
        }
        
        // Click the first order link
        await orderLinks[0].click();
        await expect(page).toHaveURL(/.*order-summary.*/);
        await expect(page.locator('h1')).toContainText('Order Summary');
    });

    test('should display correct order information', async ({ page }) => {
        // Navigate directly to an order summary page
        // This depends on having an order ID for testing
        // For this test, we'll need to get an order ID from an event first
        await page.goto('/hvac-dashboard/');
        
        // Find an event and navigate to its summary
        const eventLinks = await page.$$('a[href*="event-summary"]');
        if (eventLinks.length === 0) {
            test.skip('No events with ticket sales found');
        }
        
        await eventLinks[0].click();
        await expect(page).toHaveURL(/.*event-summary.*/);
        
        // Look for order links and get the first order's ID
        const orderLink = await page.$('a[href*="order-summary"]');
        if (!orderLink) {
            test.skip('No orders found for this event');
        }
        
        const href = await orderLink.getAttribute('href');
        const orderId = href.match(/order_id=(\d+)/)[1];
        
        // Navigate to order summary page directly
        await page.goto(`/order-summary/?order_id=${orderId}`);
        await expect(page).toHaveURL(/.*order-summary.*/);
        
        // Check that order details are displayed correctly
        await expect(page.locator('h1')).toContainText('Order Summary');
        await expect(page.locator('.hvac-details-table')).toBeVisible();
        
        // Check for order number
        const orderNumberCell = await page.locator('.hvac-details-table tr', { has: page.locator('th', { hasText: 'Order Number' }) }).locator('td');
        await expect(orderNumberCell).toBeVisible();
        
        // Check for tickets table
        await expect(page.locator('.hvac-tickets-table')).toBeVisible();
        await expect(page.locator('.hvac-tickets-table th', { hasText: 'Attendee' })).toBeVisible();
        await expect(page.locator('.hvac-tickets-table th', { hasText: 'Email' })).toBeVisible();
        await expect(page.locator('.hvac-tickets-table th', { hasText: 'Ticket Type' })).toBeVisible();
        await expect(page.locator('.hvac-tickets-table th', { hasText: 'Event' })).toBeVisible();
    });

    test('should redirect to login page when not logged in', async ({ page }) => {
        // Logout first
        await page.goto('/wp-login.php?action=logout');
        await page.click('text=log out');
        
        // Attempt to access order summary page directly
        await page.goto('/order-summary/?order_id=1');
        
        // Should redirect to login page
        await expect(page.locator('text=Authentication Required')).toBeVisible();
        await expect(page.locator('text=Please log in to view the order summary')).toBeVisible();
        await expect(page.locator('a', { hasText: 'Log In' })).toBeVisible();
    });

    test('should provide navigation back to event summary', async ({ page }) => {
        // Find an event and navigate to its summary
        await page.goto('/hvac-dashboard/');
        const eventLinks = await page.$$('a[href*="event-summary"]');
        if (eventLinks.length === 0) {
            test.skip('No events with ticket sales found');
        }
        
        await eventLinks[0].click();
        await expect(page).toHaveURL(/.*event-summary.*/);
        
        // Get the event ID from the URL
        const url = page.url();
        const eventId = url.match(/event_id=(\d+)/)[1];
        
        // Look for order links and navigate to an order
        const orderLink = await page.$('a[href*="order-summary"]');
        if (!orderLink) {
            test.skip('No orders found for this event');
        }
        
        await orderLink.click();
        await expect(page).toHaveURL(/.*order-summary.*/);
        
        // Check for "Back to Event Summary" link
        const backLink = page.locator('a', { hasText: 'Back to Event Summary' });
        await expect(backLink).toBeVisible();
        
        // Click the link and verify it goes back to the event summary
        await backLink.click();
        await expect(page).toHaveURL(new RegExp(`.*event-summary.*event_id=${eventId}`));
    });
});