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

async function navigateToEditPage(page) {
    await page.goto('/services');
    await page.waitForLoadState('networkidle');

    const editButton = page.getByTestId('service-list-edit-button').first();
    const hasEditableService = await editButton.isVisible().catch(() => false);

    if (!hasEditableService) {
        return false;
    }

    await editButton.click();
    await page.waitForLoadState('networkidle');
    return true;
}

test('navigate to first editable service edit page', async ({ page }) => {
    const navigated = await navigateToEditPage(page);
    if (!navigated) {
        test.skip();
    }

    await expect(page).toHaveURL(/.*services\/\d+\/edit/);

    const agendaSection = page.getByTestId('agenda-section');
    const emptyState = page.getByText('Keine Ablauf-Elemente vorhanden');
    const hasAgenda = await agendaSection.isVisible().catch(() => false);
    const hasEmptyState = await emptyState.isVisible().catch(() => false);

    expect(hasAgenda || hasEmptyState).toBe(true);
});

test.skip('moderation block accordion — replaced by agenda view', async () => {});

test('agenda items with slides show slide uploader', async ({ page }) => {
    const navigated = await navigateToEditPage(page);
    if (!navigated) {
        test.skip();
    }

    const agendaRows = page.getByTestId('agenda-item-row');
    const rowCount = await agendaRows.count();

    if (rowCount === 0) {
        test.skip();
    }

    const addSlidesBtn = page.getByTestId('agenda-item-add-slides').first();
    const hasSlidesBtn = await addSlidesBtn.isVisible().catch(() => false);

    if (!hasSlidesBtn) {
        test.skip();
    }

    await expect(addSlidesBtn).toBeVisible();
});

test.skip('existing moderation slides display as thumbnails — replaced by agenda item slides', async () => {});

test.skip('delete button on moderation slide triggers confirmation — replaced by agenda item slides', async () => {});
