import test, { expect } from '@playwright/test'
import { typebotViewer } from '../../services/selectorUtils'
import { importTypebotInDatabase } from '../../services/database'
import path from 'path'
import cuid from 'cuid'

test('should be configurable', async ({ page }) => {
  const typebotId = cuid()
  const linkedTypebotId = cuid()
  await importTypebotInDatabase(
    path.join(__dirname, '../../fixtures/typebots/logic/linkTypebots/1.json'),
    { id: typebotId, name: 'My link typebot 1' }
  )
  await importTypebotInDatabase(
    path.join(__dirname, '../../fixtures/typebots/logic/linkTypebots/2.json'),
    { id: linkedTypebotId, name: 'My link typebot 2' }
  )

  await page.goto(`/typebots/${typebotId}/edit`)
  await page.click('text=Configure...')
  await page.click('input[placeholder="Select a typebot"]')
  await page.click('text=My link typebot 2')
  await expect(page.locator('input[value="My link typebot 2"]')).toBeVisible()
  await page.click('[aria-label="Navigate to typebot"]')
  await expect(page).toHaveURL(
    `/typebots/${linkedTypebotId}/edit?parentId=${typebotId}`
  )
  await page.click('[aria-label="Navigate back"]')
  await expect(page).toHaveURL(`/typebots/${typebotId}/edit`)
  await page.click('text=Jump in My link typebot 2')
  await expect(page.locator('input[value="My link typebot 2"]')).toBeVisible()
  await page.click('input[placeholder="Select a block"]')
  await page.click('text=Block #2')

  await page.click('text=Preview')
  await expect(typebotViewer(page).locator('text=Second block')).toBeVisible()

  await page.click('[aria-label="Close"]')
  await page.click('text=Jump to Block #2 in My link typebot 2')
  await page.click('input[value="Block #2"]', { clickCount: 3 })
  await page.press('input[value="Block #2"]', 'Backspace')
  await page.click('button >> text=Start')

  await page.click('text=Preview')
  await typebotViewer(page).locator('input').fill('Hello there!')
  await typebotViewer(page).locator('input').press('Enter')
  await expect(typebotViewer(page).locator('text=Hello there!')).toBeVisible()

  await page.click('[aria-label="Close"]')
  await page.click('text=Jump to Start in My link typebot 2')
  await page.click('input[value="My link typebot 2"]', { clickCount: 3 })
  await page.press('input[value="My link typebot 2"]', 'Backspace')
  await page.click('button >> text=My link typebot 1')
  await page.click('input[placeholder="Select a block"]', {
    clickCount: 3,
  })
  await page.press('input[placeholder="Select a block"]', 'Backspace')
  await page.click('button >> text=Hello')

  await page.click('text=Preview')
  await expect(typebotViewer(page).locator('text=Hello world')).toBeVisible()
})
