// @vitest-environment jsdom
import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { LastUpdate } from "./LastUpdate.js";
import type { ISimpleFlight, IFlightLeg } from "../../types.js";

vi.mock("@/i18n/provider.js", () => ({ useTranslation: () => ({ t: (k: string) => k }) }));

function makeLeg(updated: string): IFlightLeg {
  return {
    arrival: {
      scheduled: { airport: "", airportCode: "LED", city: "", cityCode: "", countryCode: "" },
      latest: { airport: "", airportCode: "LED", city: "", cityCode: "", countryCode: "" },
      dispatch: "",
      gate: "",
      terminal: "",
      times: {
        scheduledArrival: {
          dayChange: { value: 0, title: "" },
          local: "",
          localTime: "",
          tzOffset: 0,
          utc: "",
        },
      },
    },
    dayChange: 0,
    departure: {
      scheduled: { airport: "", airportCode: "SVO", city: "", cityCode: "", countryCode: "" },
      latest: { airport: "", airportCode: "SVO", city: "", cityCode: "", countryCode: "" },
      dispatch: "",
      gate: "",
      terminal: "",
      checkingStatus: "Scheduled",
      parkingStand: "",
      times: {
        scheduledDeparture: {
          dayChange: { value: 0, title: "" },
          local: "",
          localTime: "",
          tzOffset: 0,
          utc: "2026-04-20T10:00:00Z",
        },
      },
    },
    equipment: {},
    flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
    flyingTime: "1h",
    index: 0,
    operatingBy: {},
    status: "Scheduled",
    updated,
  } as IFlightLeg;
}

function makeFlight(updated: string): ISimpleFlight {
  return {
    id: "X",
    routeType: "Direct",
    flyingTime: "1h",
    status: "Scheduled",
    flightId: { carrier: "SU", flightNumber: "0022", suffix: "", date: "20260417" },
    operatingBy: { carrier: "SU", flightNumber: "0022" },
    leg: makeLeg(updated),
  } as ISimpleFlight;
}

describe("LastUpdate", () => {
  it("renders LAST-UPDATE label", () => {
    render(<LastUpdate flight={makeFlight("2026-04-20T12:34:00Z")} locale="ru" />);
    expect(screen.getByText(/SHARED\.LAST-UPDATE/)).toBeTruthy();
  });

  it("renders timestamp in HH:mm dd.MM.yyyy format", () => {
    render(<LastUpdate flight={makeFlight("2026-04-20T12:34:00Z")} locale="ru" />);
    const ts = screen.getByTestId("last-update-timestamp");
    expect(ts.textContent).toMatch(/\d{2}:\d{2}\s+\d{2}\.\d{2}\.\d{4}/);
  });

  it("renders client-side load timestamp regardless of leg.updated value", () => {
    // Angular sets `flight.lastUpdate = new Date()` in populate.logic.ts,
    // so the stamp tracks when the client received the data — not the
    // API record's mutation timestamp. Empty leg.updated must NOT blank
    // the field.
    render(<LastUpdate flight={makeFlight("")} locale="ru" />);
    const ts = screen.getByTestId("last-update-timestamp");
    expect(ts.textContent).toMatch(/\d{2}:\d{2}\s+\d{2}\.\d{2}\.\d{4}/);
  });

  it("renders share button", () => {
    render(<LastUpdate flight={makeFlight("2026-04-20T12:34:00Z")} locale="ru" />);
    expect(screen.getByTestId("share-button")).toBeTruthy();
  });
});
