/**
 * Copyright (C) 2025 Brainformatik GmbH
 *
 * This file is part of brainX.
 * All Rights Reserved.
 *
 * This part of brainX can not be copied and/or distributed without the
 * express permission of Brainformatik GmbH.
 */

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

export class ContactDetailPage {
  constructor(page) {
    this.page = page;
  }

  async waitForHero(expectedName) {
    const hero = this.page.locator('[data-test="contact-hero-name"]');
    const infoTile = this.page.locator('a[href*="/contacts/"][href$="/info"]').first();
    await infoTile.waitFor({
      state: 'visible'
    });
    if (expectedName && await hero.count()) {
      const heroText = (await hero.first().textContent())?.trim() || '';
      if (heroText) {
        await expect(hero.first()).toContainText(expectedName);
      }
    }
  }

  heroName() {
    return this.page.locator('[data-test="contact-hero-name"]');
  }

  quickActions() {
    return this.page.locator('[data-test="contact-hero-actions"] .detail-quick-action, main button').filter({
      hasText: /call|email|edit/i
    });
  }

  detailLink(id) {
    return this.page.locator(`[data-test="contact-detail-link-${id}"]`);
  }

  detailLinks() {
    return this.page.locator('[data-test="contact-detail-links"] .detail-link-tile, main a[href*="/info"], main button').filter({
      hasText: /contact information|comments|activity overview/i
    });
  }

  relatedLinks() {
    return this.page.locator('[data-test="contact-related-links"] .detail-link-tile, main a').filter({
      hasText: /organization|visit reports/i
    });
  }

  visitReportsTile() {
    return this.page.locator('[data-test="contact-related-link-visitreport"], main a').filter({
      hasText: /visit reports/i
    });
  }

  errorBanner() {
    return this.page.locator('[data-test="contact-detail-error"]');
  }

  async goBack() {
    await this.page.goBack();
  }

  async reload() {
    await this.page.reload();
  }
}
