import { Given, When, Then } from '@cucumber/cucumber'
import { expect } from '@playwright/test'
import type { AppWorld } from '../support/world.js'

/**
 * Generic step library — shared across every project that copies this harness.
 *
 * Project-specific business steps (e.g. "I create a booking for <date>")
 * belong in their own *.steps.ts files. Keep this file framework-agnostic.
 */

Given('I visit {string}', async function (this: AppWorld, path: string) {
  await this.gotoApp(path)
})

Given(
  'I am logged in as {string} with password {string}',
  async function (this: AppWorld, email: string, password: string) {
    await this.login(email, password)
  },
)

When('I log in as {string} with password {string}', async function (
  this: AppWorld,
  email: string,
  password: string,
) {
  await this.login(email, password)
})

When('I log out', async function (this: AppWorld) {
  await this.logout()
})

When(
  'I fill {string} with {string}',
  async function (this: AppWorld, selector: string, value: string) {
    await this.page.locator(selector).fill(value)
  },
)

When('I click {string}', async function (this: AppWorld, label: string) {
  await this.page.getByRole('button', { name: label }).first().click()
})

Then('I see {string}', async function (this: AppWorld, text: string) {
  await this.expectText(text)
})

Then('I do not see {string}', async function (this: AppWorld, text: string) {
  await expect(
    this.page.getByText(text, { exact: false }).first(),
  ).toBeHidden()
})

Then('the URL contains {string}', async function (this: AppWorld, fragment: string) {
  await this.page.waitForURL((url) => url.toString().includes(fragment))
})
