/**
 * Unit tests for ContactLinks: phone/Telegram display, labels, layout.
 * Ported from webapp/js/contactHtml.test.js buildContactLinksHtml.
 */

import { describe, it, expect, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import { ContactLinks } from "./ContactLinks";
import { resetAppStore } from "@/test/test-utils";

describe("ContactLinks", () => {
  beforeEach(() => {
    resetAppStore();
  });

  it("returns null when phone and username are missing", () => {
    const { container } = render(
      <ContactLinks phone={null} username={null} />
    );
    expect(container.firstChild).toBeNull();
  });

  it("renders phone only with label and tel: link", () => {
    render(<ContactLinks phone="+79991234567" username={null} showLabels />);
    expect(document.querySelector('a[href^="tel:"]')).toBeInTheDocument();
    expect(screen.getByText(/Phone/i)).toBeInTheDocument();
  });

  it("displays phone formatted for Russian numbers", () => {
    render(<ContactLinks phone="79146522209" username={null} />);
    expect(screen.getByText(/\+7 914 652-22-09/)).toBeInTheDocument();
  });

  it("renders username only with label and t.me link", () => {
    render(<ContactLinks phone={null} username="alice_dev" showLabels />);
    expect(document.querySelector('a[href*="t.me"]')).toBeInTheDocument();
    expect(screen.getByText(/alice_dev/)).toBeInTheDocument();
  });

  it("renders both phone and username with labels", () => {
    render(
      <ContactLinks
        phone="+79001112233"
        username="bob"
        showLabels
      />
    );
    expect(document.querySelector('a[href^="tel:"]')).toBeInTheDocument();
    expect(document.querySelector('a[href*="t.me"]')).toBeInTheDocument();
    expect(screen.getByText(/\+7 900 111-22-33/)).toBeInTheDocument();
    expect(screen.getByText(/@bob/)).toBeInTheDocument();
  });

  it("strips leading @ from username and displays with @", () => {
    render(<ContactLinks phone={null} username="@alice" />);
    const link = document.querySelector('a[href*="t.me/alice"]');
    expect(link).toBeInTheDocument();
    expect(link?.textContent).toContain("@alice");
  });
});
