// page-objects/ProfileApplicantPage.ts

import { Page } from '@playwright/test';

export class ProfileApplicantPage {
  readonly page: Page;

  constructor(page: Page) {
    this.page = page;
  }

  async goto() {
    await this.page.goto('/account/profile');
  }

  async openBasicInfoEditor() {
    await this.page.getByTestId('btn-pensil').nth(0).click();
  }

  async uploadAvatar(filePath: string) {
    const [fileChooser] = await Promise.all([    
      this.page.waitForEvent('filechooser'),
      this.page.getByTestId('ModalUserInfo').getByTestId('btn-pensil').first().click(),      
    ]);
    await fileChooser.setFiles(filePath);
  }

  async fillCity(city: string) {
    await this.page.fill('input[name="city"]', city);
  }

  async saveBasicInfo() {
    await this.page.getByTestId('btn-save').click();
  }

  async openRepresentativeEditor() {
    await this.page.getByTestId('btn-pensil').nth(1).click();
  }

  async fillRepresentative(from: string, fullName: string, phone: string) {
    await this.page.fill('input[name="parents.0.from"]', from);
    await this.page.fill('input[name="parents.0.fio"]', fullName);
    await this.page.fill('input[name="parents.0.phone"]', phone);
  }

  async saveRepresentative() {
    await this.page.locator('button:has-text("Сохранить")').click();
  }

  async openDisabilityEditor() {
    await this.page.getByTestId('btn-pensil').nth(2).click();
  }

  async selectDisability(disability: string) {
    await this.page.getByText(disability).click();
  }

  async selectDisabilityGroup(group: string) {
    await this.page.getByText(group).first().click();
  }

  async saveDisabilityInfo() {
    await this.page.locator('button:has-text("Сохранить")').click();
  }
}
