import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import { BasicInfoTab, formatSizeCategory } from "../portal/sections/PetProfiles.js";
import type { Pet } from "@groombook/types";

// The portal endpoint (GET /api/portal/pets) serializes DB columns under
// portal-shaped keys: weightKg→weight, dateOfBirth→birthDate. The read view
// must surface those keys (GRO-2207), not the raw staff-side weightKg/dateOfBirth.
const PORTAL_PET: Pet = {
  id: "pet-1",
  clientId: "client-1",
  name: "Pup Alpha",
  species: "dog",
  breed: "Poodle",
  // Staff-shaped keys intentionally null — only the portal keys are populated,
  // proving the read view reads `weight`/`birthDate`.
  weightKg: null,
  dateOfBirth: null,
  weight: "12.50",
  birthDate: "2022-03-10T00:00:00.000Z",
  petSizeCategory: "extra_large",
  healthAlerts: null,
  groomingNotes: null,
  cutStyle: null,
  shampooPreference: null,
  specialCareNotes: null,
  customFields: {},
  coatType: null,
  preferredCuts: [],
  medicalAlerts: [],
  createdAt: "2024-01-01T00:00:00.000Z",
  updatedAt: "2024-01-01T00:00:00.000Z",
};

describe("BasicInfoTab read view (GRO-2207)", () => {
  it("renders Weight from the portal `weight` key", () => {
    render(<BasicInfoTab pet={PORTAL_PET} readOnly />);
    expect(screen.getByText("12.50 kg")).toBeInTheDocument();
  });

  it("renders Date of Birth from the portal `birthDate` key", () => {
    render(<BasicInfoTab pet={PORTAL_PET} readOnly />);
    expect(screen.getByText("March 10, 2022")).toBeInTheDocument();
  });

  it("renders Size Category formatted from petSizeCategory", () => {
    render(<BasicInfoTab pet={PORTAL_PET} readOnly />);
    expect(screen.getByText("Size Category")).toBeInTheDocument();
    expect(screen.getByText("Extra Large")).toBeInTheDocument();
  });

  it("falls back to staff-shaped keys when portal keys are absent", () => {
    const staffShaped: Pet = { ...PORTAL_PET, weight: null, birthDate: null, weightKg: 25, dateOfBirth: "2020-01-05T00:00:00.000Z" };
    render(<BasicInfoTab pet={staffShaped} readOnly />);
    expect(screen.getByText("25 kg")).toBeInTheDocument();
    expect(screen.getByText("January 5, 2020")).toBeInTheDocument();
  });

  it("shows Unknown for missing weight/DoB and size", () => {
    const empty: Pet = { ...PORTAL_PET, weight: null, birthDate: null, weightKg: null, dateOfBirth: null, petSizeCategory: null };
    render(<BasicInfoTab pet={empty} readOnly />);
    // Weight, Date of Birth and Size Category rows all read "Unknown".
    expect(screen.getAllByText("Unknown").length).toBeGreaterThanOrEqual(3);
  });
});

describe("formatSizeCategory", () => {
  it("title-cases each underscore-separated segment", () => {
    expect(formatSizeCategory("extra_large")).toBe("Extra Large");
    expect(formatSizeCategory("small")).toBe("Small");
    expect(formatSizeCategory("medium")).toBe("Medium");
    expect(formatSizeCategory("large")).toBe("Large");
  });

  it("returns Unknown for null/undefined/empty", () => {
    expect(formatSizeCategory(null)).toBe("Unknown");
    expect(formatSizeCategory(undefined)).toBe("Unknown");
    expect(formatSizeCategory("")).toBe("Unknown");
  });
});
