import { render, screen } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import { UserPanel } from "@/components/entities/user/UserPanel";
import { CustomerPanel } from "@/components/entities/customer/CustomerPanel";
import { AttributePanel } from "@/components/entities/attribute/AttributePanel";
import { BrandPanel } from "@/components/entities/brand/BrandPanel";
import { ManufacturerPanel } from "@/components/entities/manufacturer/ManufacturerPanel";
import { TaxClassPanel } from "@/components/entities/tax-class/TaxClassPanel";
import React from "react";

// Mock formatCurrency to avoid issues with locale in tests
vi.mock("@/lib/format", () => ({
  formatCurrency: (value: number) => `$${value}`,
  formatDate: (date: string) => date,
  formatDateTime: (date: string) => date,
}));

describe("Entity Panels", () => {
  describe("UserPanel", () => {
    const mockUserData = {
      id: "user-1",
      firstName: "John",
      lastName: "Doe",
      email: "john@example.com",
      role: "admin",
      is_active: true,
      last_login: "2023-01-01T10:00:00Z",
      created_at: "2022-01-01T10:00:00Z",
      updated_at: "2022-01-01T10:00:00Z",
    };

    it("renders user information correctly", () => {
      render(<UserPanel data={mockUserData} />);

      expect(screen.getByRole("heading", { name: /john doe/i })).toBeInTheDocument();
      expect(screen.getByText(/Rolle:/i)).toBeInTheDocument();
      // "admin" appears once in the standardized DetailInfoItem
      expect(screen.getByText("admin")).toBeInTheDocument();
      expect(screen.getByText(/Status:/i)).toBeInTheDocument();
      // Match "Aktiv" specifically to avoid matching "Aktivität"
      expect(screen.getByText(/^aktiv$/i)).toBeInTheDocument();
      expect(screen.getByText(/letzter login/i)).toBeInTheDocument();
    });

    it("handles missing optional data", () => {
      const minimalData = {
        id: "user-2",
        firstName: "Jane",
        lastName: "Smith",
        email: "jane@example.com",
        role: "user",
      };
      render(<UserPanel data={minimalData} />);

      expect(screen.getByRole("heading", { name: /jane smith/i })).toBeInTheDocument();
      expect(screen.queryByText(/letzter login/i)).not.toBeInTheDocument();
    });

    it("handles missing names and inactive status", () => {
      const data = {
        id: "user-3",
        email: "no-name@example.com",
        role: "user",
        is_active: false,
      };
      render(<UserPanel data={data} />);

      // Should use email as fallback for name if first/last name missing
      expect(screen.getByRole("heading", { name: /no-name@example.com/i })).toBeInTheDocument();
      // Status should be "Inaktiv"
      expect(screen.getByText(/inaktiv/i)).toBeInTheDocument();
    });

    it("handles partial names", () => {
      const data = {
        id: "user-4",
        firstName: "OnlyFirst",
        email: "partial@example.com",
        role: "user",
      };
      render(<UserPanel data={data} />);
      expect(screen.getByRole("heading", { name: /onlyfirst/i })).toBeInTheDocument();
    });

    it("handles lastName only", () => {
      const data = {
        id: "user-5",
        lastName: "OnlyLast",
        email: "last-only@example.com",
        role: "user",
      };
      render(<UserPanel data={data} />);
      expect(screen.getByRole("heading", { name: /onlylast/i })).toBeInTheDocument();
    });
  });

  describe("CustomerPanel", () => {
    const mockCustomerData = {
      id: "customer-1",
      name: "Acme Corp",
      email: "contact@acme.com",
      phone_number: "123456",
      address: "Main St 1",
      total_orders: 5,
      total_spent: 500,
      created_at: "2022-01-01T10:00:00Z",
      updated_at: "2022-01-01T10:00:00Z",
      last_order_date: "2023-01-01T10:00:00Z",
      last_order_id: "order-100",
    };

    it("renders customer information correctly", () => {
      render(<CustomerPanel data={mockCustomerData} />);

      expect(screen.getByText("Acme Corp")).toBeDefined();
      expect(screen.getByText("Gesamtbestellungen:")).toBeDefined();
      expect(screen.getByText("5")).toBeDefined();
      expect(screen.getByText("Gesamtausgaben:")).toBeDefined();
      expect(screen.getByText("$500")).toBeDefined();
    });

    it("handles minimal customer data", () => {
      const minimalData = {
        id: "customer-2",
        name: "Minimal Inc",
      };
      render(<CustomerPanel data={minimalData} />);

      expect(screen.getByText("Minimal Inc")).toBeDefined();
      expect(screen.getByText("0")).toBeDefined(); // total_orders defaults to 0
      expect(screen.getByText("$0")).toBeDefined(); // total_spent defaults to 0
    });
  });

  describe("AttributePanel", () => {
    const mockAttributeData = {
      id: "attr-1",
      name: "Farbe",
      type: "select",
      description: "Die Primärfarbe des Produkts",
      is_active: true,
      created_at: "2023-01-01T10:00:00Z",
      updated_at: "2023-01-01T10:00:00Z",
    };

    it("renders attribute information correctly", () => {
      render(<AttributePanel data={mockAttributeData} />);

      expect(screen.getByText("Farbe")).toBeDefined();
      expect(screen.getByText("Attribut")).toBeDefined();
      expect(screen.getByText("Details")).toBeDefined();
      expect(screen.getByText(/Typ: Auswahl/)).toBeDefined();
      expect(screen.getByText("Die Primärfarbe des Produkts")).toBeDefined();
      expect(screen.getByText("Aktiv")).toBeDefined();
    });
  });

  describe("BrandPanel", () => {
    const mockBrandData = {
      id: "brand-1",
      name: "Apple",
      description: "Technology company",
      is_active: true,
      created_at: "2023-01-01T10:00:00Z",
      updated_at: "2023-01-01T10:00:00Z",
    };

    it("renders brand information correctly", () => {
      render(<BrandPanel data={mockBrandData} />);

      expect(screen.getByText("Apple")).toBeDefined();
      expect(screen.getByText("Marke")).toBeDefined();
      expect(screen.getByText("Details")).toBeDefined();
      expect(screen.getByText("Technology company")).toBeDefined();
      expect(screen.getByText("Aktiv")).toBeDefined();
    });
  });

  describe("ManufacturerPanel", () => {
    const mockData = {
      id: "m-1",
      companyName: "Tesla",
      email: "contact@tesla.com",
      phoneNumber: "123-456",
      address: "123 Palo Alto",
      createdAt: "2023-01-01T10:00:00Z",
      updatedAt: "2023-01-01T10:00:00Z",
    };

    it("renders manufacturer information correctly", () => {
      render(<ManufacturerPanel data={mockData} />);

      expect(screen.getByText("Tesla")).toBeDefined();
      expect(screen.getByText("Hersteller")).toBeDefined();
      expect(screen.getByText("contact@tesla.com")).toBeDefined();
      expect(screen.getByText("123-456")).toBeDefined();
      expect(screen.getByText("123 Palo Alto")).toBeDefined();
    });
  });

  describe("TaxClassPanel", () => {
    const mockData = {
      id: "tc-1",
      name: "Standard",
      percentage: 19,
      created_at: "2023-01-01T10:00:00Z",
      updated_at: "2023-01-01T10:00:00Z",
    };

    it("renders tax class information correctly", () => {
      render(<TaxClassPanel data={mockData} />);

      expect(screen.getByText("Standard")).toBeDefined();
      expect(screen.getByText("Steuerklasse")).toBeDefined();
      expect(screen.getByText("19% Steuersatz")).toBeDefined();
    });
  });
});
