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

test('api keys page opens create key flow', async ({ page }) => {
  await login(page);
  const workspace = await getCurrentWorkspace(page);
  await createAgent(page, workspace.id, {
    slug: uniqueName('playwright_keys_agent'),
    display_name: 'Playwright Keys Agent',
    description: 'Agent for API key page smoke test.',
    instructions: {},
    tool_selection_policy: {},
  });
  await page.goto('/api-keys');
  await expect(page.locator('.page-title')).toHaveText(localized('Agent Keys', 'Ключи агентов'));
  await expect(page.locator('[data-testid="machine-access-summary"]')).toContainText(
    localized('static AI-agent keys', 'статические ключи AI-агентов')
  );
  await expect(page.locator('#machine-access-note')).toContainText(
    localized('commercial edition', 'коммерческую редакцию')
  );
  await expect(page.locator('#agent-select')).not.toBeDisabled();
  await expect(page.locator('#btn-create-key')).toBeEnabled();
  await page.locator('#btn-create-key').click();
  await expect(page.locator('#modal-create')).toHaveClass(/open/);
  await expect(page.locator('.modal-title')).toHaveText(localized('Create agent key', 'Создать ключ агента'));
  await page.locator('#new-key-name').fill(`playwright-${Date.now()}`);
  await page.locator('#modal-confirm-btn').click();
  await expect(page.locator('#modal-reveal-body')).toContainText(localized('Copy this key now', 'Скопируйте этот ключ сейчас'));
});
