/**
 * Unit tests for DayDetailContent: sorts duties by start_at; duty entries show time and name only (no contact links).
 * Ported from webapp/js/dayDetail.test.js buildDayDetailContent.
 */

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

function duty(
  full_name: string,
  start_at: string,
  end_at: string,
  extra: Partial<DutyWithUser> = {}
): DutyWithUser {
  return {
    id: 1,
    user_id: 1,
    full_name,
    start_at,
    end_at,
    event_type: "duty",
    phone: null,
    username: null,
    ...extra,
  };
}

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

  it("sorts duty list by start_at when input order is wrong", () => {
    const dateKey = "2025-02-25";
    const duties = [
      duty("Петров", "2025-02-25T14:00:00Z", "2025-02-25T18:00:00Z", { id: 2 }),
      duty("Иванов", "2025-02-25T09:00:00Z", "2025-02-25T14:00:00Z", { id: 1 }),
    ];
    render(
      <DayDetailContent
        dateKey={dateKey}
        duties={duties}
        eventSummaries={[]}
      />
    );
    expect(screen.getByText("Иванов")).toBeInTheDocument();
    expect(screen.getByText("Петров")).toBeInTheDocument();
    const body = document.body.innerHTML;
    const ivanovPos = body.indexOf("Иванов");
    const petrovPos = body.indexOf("Петров");
    expect(ivanovPos).toBeLessThan(petrovPos);
  });

  it("shows duty time and name on one line and does not show contact links", () => {
    const dateKey = "2025-03-01";
    const duties = [
      duty("Alice", "2025-03-01T09:00:00Z", "2025-03-01T17:00:00Z", {
        phone: "+79991234567",
        username: "alice_dev",
      }),
    ];
    render(
      <DayDetailContent
        dateKey={dateKey}
        duties={duties}
        eventSummaries={[]}
      />
    );
    expect(screen.getByText("Alice")).toBeInTheDocument();
    expect(document.querySelector('a[href^="tel:"]')).toBeNull();
    expect(document.querySelector('a[href*="t.me"]')).toBeNull();
    expect(screen.queryByText(/alice_dev/)).not.toBeInTheDocument();
  });
});
