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

import { isolatedPerTest as test } from '../fixtures/test.js';

test.describe('Drafts', () => {
  test('Draft ID is successfully stored in Drupal on Guidance', async ({ conductor, page }) => {
    const app = await conductor.open();

    await conductor.initializeKeywordsAndCompetitorUrls(app);

    await expect(app.getByRole('tab', { name: 'Guidance' })).toBeVisible();
    await app.getByRole('tab', { name: 'Guidance' }).click();
    await page.locator('.loading').waitFor({ state: 'detached' });
    await expect(app.locator('summary', { hasText: 'Objective' })).toBeVisible();
    await page.waitForLoadState('networkidle');

    const parts = new URL(page.url()).pathname.split('/').filter(Boolean);
    const entityType = parts[2];
    const entityId = parts[3];
    const response = await page.request.get(`/conductor/api/draft/${entityType}/${entityId}`);
    expect(response.ok()).toBeTruthy();
    const draft = await response.json();
    expect(draft.draft_id).toBe('bead88f1-bd37-48a7-9861-46f04058141d');
  });

  test('Draft ID is successfully stored in Drupal on Content Outline', async ({
    conductor,
    page,
  }) => {
    const app = await conductor.open();

    await conductor.initializeKeywordsAndCompetitorUrls(app);

    await expect(app.getByRole('tab', { name: 'Content outline' })).toBeVisible();
    await app.getByRole('tab', { name: 'Content outline' }).click();
    await page.locator('.loading').waitFor({ state: 'detached' });
    await expect(app.getByText('Title tag')).toBeVisible();
    await page.waitForLoadState('networkidle');

    const parts = new URL(page.url()).pathname.split('/').filter(Boolean);
    const entityType = parts[2];
    const entityId = parts[3];
    const response = await page.request.get(`/conductor/api/draft/${entityType}/${entityId}`);
    expect(response.ok()).toBeTruthy();
    const draft = await response.json();
    expect(draft.draft_id).toBe('bead88f1-bd37-48a7-9861-46f04058141d');
  });
});
