import type { Page } from "@playwright/test";

export class UserCreateFormPage {
  private page: Page;
  constructor(page: Page) {
    this.page = page;
  }

  async goto() {
    await this.page.goto("/user-management/user/create");
  }

  get nameInput() {
    return this.page.getByLabel("Name");
  }

  get emailInput() {
    return this.page.getByLabel("Email");
  }

  get roleSelect() {
    return this.page.getByRole("combobox").first();
  }

  roleOption(name: string | RegExp) {
    return this.page.getByRole("option", { name });
  }

  get submitButton() {
    return this.page.getByRole("button", { name: /create|save/i });
  }

  get cancelButton() {
    return this.page.getByRole("button", { name: /cancel/i });
  }

  get successDialog() {
    return this.page.getByRole("dialog", { name: /user created/i });
  }

  get doneButton() {
    return this.page.getByRole("button", { name: /^done$/i });
  }

  async fill(fields: { name?: string; email?: string }) {
    if (fields.name) await this.nameInput.fill(fields.name);
    if (fields.email) await this.emailInput.fill(fields.email);
  }
}
