/**
 * Dashboard Greeting Component Tests
 *
 * Tests that the dashboard properly displays personalized greetings
 * with the user's actual name instead of generic fallback text.
 *
 * Prevents regression of "Welcome back, Partner" bug.
 */

import React from "react";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";

// Mock next/navigation
jest.mock("next/navigation", () => ({
  useRouter: () => ({
    push: jest.fn(),
    replace: jest.fn(),
    prefetch: jest.fn(),
  }),
}));

// Mock next-auth/react
jest.mock("next-auth/react", () => ({
  signOut: jest.fn(),
}));

// Mock the config
jest.mock("@/lib/config", () => ({
  SITE: {
    name: "Mawidi",
    url: "https://mawidi.com",
  },
  UI: {
    en: {
      t: {},
      dir: "ltr",
    },
    ar: {
      t: {},
      dir: "rtl",
    },
  },
}));

/**
 * Simplified greeting component for testing
 * This mimics the greeting logic in DashboardClient
 */
interface GreetingProps {
  session: {
    user: {
      id: string;
      email: string;
      name?: string | null;
    };
  };
  lang: "en" | "ar";
}

function DashboardGreeting({ session, lang }: GreetingProps) {
  const isAr = lang === "ar";

  return (
    <div>
      <h1 data-testid="greeting">
        {isAr
          ? `مرحباً، ${(session.user && session.user.name) || "فريق ماويدي"}`
          : `Welcome back, ${(session.user && session.user.name) || "Mawidi Partner"}`}
      </h1>
    </div>
  );
}

describe("Dashboard Greeting", () => {
  describe("English Language", () => {
    test("displays user name when available", () => {
      const session = {
        user: {
          id: "user-123",
          email: "john@example.com",
          name: "John Doe",
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("Welcome back, John Doe");
      expect(greeting).not.toHaveTextContent("Partner");
    });

    test("displays fallback when name is null", () => {
      const session = {
        user: {
          id: "user-123",
          email: "john@example.com",
          name: null,
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("Welcome back, Mawidi Partner");
    });

    test("displays fallback when name is undefined", () => {
      const session = {
        user: {
          id: "user-123",
          email: "john@example.com",
          name: undefined,
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("Welcome back, Mawidi Partner");
    });

    test("displays fallback when name is empty string", () => {
      const session = {
        user: {
          id: "user-123",
          email: "john@example.com",
          name: "",
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("Welcome back, Mawidi Partner");
    });

    test("handles long names correctly", () => {
      const session = {
        user: {
          id: "user-123",
          email: "john@example.com",
          name: "Dr. John Alexander Bartholomew Smith III",
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent(
        "Welcome back, Dr. John Alexander Bartholomew Smith III",
      );
    });

    test("handles names with special characters", () => {
      const session = {
        user: {
          id: "user-123",
          email: "oconnor@example.com",
          name: "Patrick O'Connor-Smith",
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent(
        "Welcome back, Patrick O'Connor-Smith",
      );
    });
  });

  describe("Arabic Language", () => {
    test("displays user name when available", () => {
      const session = {
        user: {
          id: "user-123",
          email: "ahmad@example.com",
          name: "أحمد محمد",
        },
      };

      render(<DashboardGreeting session={session} lang="ar" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("مرحباً، أحمد محمد");
      expect(greeting).not.toHaveTextContent("فريق ماويدي");
    });

    test("displays fallback when name is null", () => {
      const session = {
        user: {
          id: "user-123",
          email: "ahmad@example.com",
          name: null,
        },
      };

      render(<DashboardGreeting session={session} lang="ar" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("مرحباً، فريق ماويدي");
    });

    test("displays English name in Arabic greeting", () => {
      const session = {
        user: {
          id: "user-123",
          email: "john@example.com",
          name: "John Doe",
        },
      };

      render(<DashboardGreeting session={session} lang="ar" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("مرحباً، John Doe");
    });
  });

  describe("Edge Cases", () => {
    test("handles whitespace-only name", () => {
      const session = {
        user: {
          id: "user-123",
          email: "john@example.com",
          name: "   ", // Only whitespace
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      // Whitespace is truthy, so it will display (though this is edge case)
      expect(greeting).toHaveTextContent("Welcome back,");
    });

    test("handles numeric characters in name", () => {
      const session = {
        user: {
          id: "user-123",
          email: "agent@example.com",
          name: "Agent 007",
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("Welcome back, Agent 007");
    });

    test("handles emoji in name", () => {
      const session = {
        user: {
          id: "user-123",
          email: "happy@example.com",
          name: "Happy User 😊",
        },
      };

      render(<DashboardGreeting session={session} lang="en" />);

      const greeting = screen.getByTestId("greeting");
      expect(greeting).toHaveTextContent("Welcome back, Happy User 😊");
    });
  });
});

describe("Session Object Structure", () => {
  test("session with complete user object displays name", () => {
    const session = {
      user: {
        id: "user-123",
        email: "john@example.com",
        name: "John Doe",
        image: "https://example.com/avatar.jpg",
      },
      expires: "2024-12-31T23:59:59.999Z",
    };

    render(<DashboardGreeting session={session} lang="en" />);

    const greeting = screen.getByTestId("greeting");
    expect(greeting).toHaveTextContent("Welcome back, John Doe");
  });

  test("session with minimal user object handles missing name", () => {
    const session = {
      user: {
        id: "user-123",
        email: "john@example.com",
      },
    };

    render(
      <DashboardGreeting
        session={session as GreetingProps["session"]}
        lang="en"
      />,
    );

    const greeting = screen.getByTestId("greeting");
    expect(greeting).toHaveTextContent("Welcome back, Mawidi Partner");
  });
});
