import { test, expect } from '@playwright/test'
import { AuthHelper, generateTestEmail, generateTestPassword, TEST_LANGUAGE } from '../helpers/auth'
import { createEmailHelper } from '../helpers/email'

test.describe('Danger Zone', () => {
  let testEmail: string
  let testPassword: string

  test.beforeEach(async ({ page, request }) => {
    // Clean email inbox before each test
    const emailHelper = createEmailHelper(request)
    await emailHelper.deleteAllMessages()

    // Create unique test user for each test
    testEmail = generateTestEmail('danger-zone')
    testPassword = generateTestPassword()

    const auth = new AuthHelper(page)

    // 1. Register the user
    await auth.register(testEmail, testPassword, 'Danger Zone Test User')
    await page.waitForLoadState('networkidle')

    // 2. Wait for verification email and get verification link
    // Use longer timeout for CI environments
    const verificationEmail = await emailHelper.waitForEmail(testEmail, {
      subject: /verify/i,
      timeout: 30000
    })
    const verificationLink = await emailHelper.getVerificationLink(verificationEmail.ID)

    if (!verificationLink) {
      throw new Error(`No verification link found in email. Email ID: ${verificationEmail.ID}`)
    }

    // 3. Click verification link
    await page.goto(verificationLink)
    await page.waitForLoadState('networkidle')

    // 4. User should now be verified and logged in automatically
    // Wait for redirect to dashboard (autoSignInAfterVerification: true)
    await page.waitForURL('**/dashboard**', { timeout: 10000 })
  })

  test('renders danger zone section when authenticated', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await expect(page.getByText(TEST_LANGUAGE.dangerZone).first()).toBeVisible()
    await expect(page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount })).toBeVisible()
  })

  test('delete button opens confirmation modal', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount }).click()

    // Modal should open - looking for Dutch text
    await expect(
      page.getByText(/Deze actie kan niet ongedaan worden gemaakt/i)
    ).toBeVisible()
  })

  test('modal shows warning message', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount }).click()

    // Should show warning - looking for Dutch text in modal (not the danger zone description)
    await expect(
      page.locator('div[role="dialog"]').getByText(/Al je gegevens worden permanent verwijderd/i)
    ).toBeVisible()
  })

  test('delete button disabled without confirmation text', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount }).click()

    // Modal delete button should be disabled
    const modalDeleteBtn = page.locator('div[role="dialog"]').getByRole('button', { name: TEST_LANGUAGE.deleteAccount })
    await expect(modalDeleteBtn).toBeDisabled()
  })

  test('delete button enabled when typing "delete"', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount }).click()

    // Type "delete"
    await page.fill('input[placeholder="delete"]', 'delete')

    // Now should be enabled
    const modalDeleteBtn = page.locator('div[role="dialog"]').getByRole('button', { name: TEST_LANGUAGE.deleteAccount })
    await expect(modalDeleteBtn).toBeEnabled()
  })

  test('delete button disabled with wrong confirmation text', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount }).click()

    // Type wrong text
    await page.fill('input[placeholder="delete"]', 'wrong')

    // Should still be disabled
    const modalDeleteBtn = page.locator('div[role="dialog"]').getByRole('button', { name: TEST_LANGUAGE.deleteAccount })
    await expect(modalDeleteBtn).toBeDisabled()
  })

  test('cancel button closes modal', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount }).click()

    // Click cancel
    await page.getByRole('button', { name: 'Annuleren' }).click()

    // Modal should close
    await expect(
      page.getByText('This action cannot be undone')
    ).not.toBeVisible()
  })

  test('shows confirmation input field', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount }).click()

    // Should have confirmation input
    await expect(
      page.locator('input[placeholder="delete"]')
    ).toBeVisible()
  })

  test('shows labels explaining what to type', async ({ page }) => {
    await page.goto('/dashboard/account/settings')
    await page.waitForLoadState('networkidle')

    await page.getByRole('button', { name: TEST_LANGUAGE.deleteAccount }).click()

    // Should show instruction to type 'delete' - looking for Dutch text
    await expect(
      page.getByText(/Typ.*delete.*om te bevestigen/i)
    ).toBeVisible()
  })
})
