const { test, expect } = require('@playwright/test');
const {
  SOAP_TEST_WSDL,
  buildSoapOperationPayload,
  createOperation,
  getCurrentWorkspace,
  login,
  localized,
  uniqueName,
} = require('./helpers');

test('wizard loads and protocol selection updates flow', async ({ page }) => {
  await login(page);
  await page.goto('/wizard/');
  await expect(page.locator('[data-step-counter]')).toContainText(localized('Step', 'Шаг'));
  await expect(page.locator('#step-panel-1 .step-panel-title')).toContainText(localized('Choose a protocol', 'Выберите протокол'));
  await page.getByText(/graphql/i).first().click();
  await page.locator('#btn-continue').click();
  await expect(page.locator('[data-step-counter]')).toContainText(/2/);
  await expect(page.locator('#step-panel-2 .step-panel-title')).toContainText(localized('Select upstream', 'Выберите upstream'));
});

test('soap wizard uploads wsdl, applies discovered binding and runs test', async ({ page }) => {
  await login(page);
  const workspace = await getCurrentWorkspace(page);
  const operation = await createOperation(
    page,
    workspace.id,
    buildSoapOperationPayload(uniqueName('playwright_soap_wizard')),
  );

  await page.goto(`/wizard/?mode=edit&operationId=${encodeURIComponent(operation.operation_id)}`);
  await page.locator('#btn-continue').click();
  await page.locator('#btn-continue').click();

  await expect(page.locator('#step-panel-3-soap .step-panel-title')).toContainText(/WSDL/i);
  await page.evaluate((wsdl) => {
    const fileName = document.getElementById('wizard-soap-wsdl-name');
    if (fileName) fileName.textContent = 'lead.wsdl';
  }, SOAP_TEST_WSDL);
  await page.evaluate(async ({ workspaceId, operationId, wsdl }) => {
    const uploaded = await window.CrankApi.uploadWsdlFile(
      workspaceId,
      operationId,
      new TextEncoder().encode(wsdl),
      'lead.wsdl',
    );
    const services = await window.CrankApi.listSoapServices(
      workspaceId,
      operationId,
      null,
    );
    window.renderSoapServiceCatalog(services.services || []);
    return uploaded;
  }, {
    workspaceId: workspace.id,
    operationId: operation.operation_id,
    wsdl: SOAP_TEST_WSDL,
  });

  await expect(page.locator('#wizard-soap-services-list button')).toContainText('CreateLead');
  await page.locator('#wizard-soap-services-list button').getByText('CreateLead').click();
  await expect(page.locator('#soap-service-name')).toHaveValue('LeadService');
  await expect(page.locator('#soap-port-name')).toHaveValue('LeadPort');
  await expect(page.locator('#soap-operation-name')).toHaveValue('CreateLead');

  await page.locator('#btn-continue').click();
  await expect(page.locator('#step-panel-4 .step-panel-title')).toBeVisible();
  await page.locator('#tool-input-schema').fill(JSON.stringify({
    type: 'object',
    required: ['email'],
    properties: {
      email: { type: 'string' },
    },
  }, null, 2));
  await page.locator('#tool-output-schema').fill(JSON.stringify({
    type: 'object',
    required: ['id'],
    properties: {
      id: { type: 'string' },
    },
  }, null, 2));
  await page.locator('#btn-continue').click();
  await expect(page.locator('[data-i18n="wizard.step5.test_title"]')).toContainText(localized('Test run', 'Тестовый запуск'));
  await page.locator('#tool-input-mapping').fill('email: "$.input.email"');
  await page.locator('#tool-output-mapping').fill('id: "$.response.body.id"');
  await page.locator('#tool-exec-config').fill('timeout_ms: 1000');

  await page.locator('#wizard-test-input').fill(JSON.stringify({ email: 'user@example.com' }, null, 2));
  await page.locator('#wizard-run-test').click();

  await expect(page.locator('#wizard-test-request-preview')).toHaveValue(/user@example.com/);
  await expect(page.locator('#wizard-test-response-preview')).toHaveValue(/lead_soap_123/);
  await expect(page.locator('#wizard-test-errors')).toHaveValue('[]');
});
