/**
 * Unit tests for components/blog/PostCard.tsx
 * Tests blog post card rendering, date formatting, bilingual support
 */

import React from "react";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import PostCard from "@/components/blog/PostCard";
import type { BlogMetadata } from "@/lib/blog.types";

const mockPost: BlogMetadata = {
  slug: "test-post",
  title: "Test Blog Post",
  date: "2024-01-15",
  excerpt:
    "This is a test excerpt with some content to display in the card preview section.",
  coverImage: "/images/test.jpg",
  tags: ["ai", "automation"],
  author: "John Doe",
  published: true,
  readingTime: "5",
  lang: "en",
};

// Component text patterns changed (Read More, date format, tag icons) - needs rewrite
describe.skip("PostCard Component", () => {
  describe("Rendering", () => {
    it("should render post title", () => {
      render(<PostCard post={mockPost} lang="en" />);

      expect(screen.getByText("Test Blog Post")).toBeInTheDocument();
    });

    it("should render post excerpt", () => {
      render(<PostCard post={mockPost} lang="en" />);

      expect(screen.getByText(/test excerpt/i)).toBeInTheDocument();
    });

    it("should render author name", () => {
      render(<PostCard post={mockPost} lang="en" />);

      expect(screen.getByText("John Doe")).toBeInTheDocument();
    });

    it("should render reading time", () => {
      render(<PostCard post={mockPost} lang="en" />);

      expect(screen.getByText("5")).toBeInTheDocument();
    });

    it("should render first tag", () => {
      render(<PostCard post={mockPost} lang="en" />);

      expect(screen.getByText("ai")).toBeInTheDocument();
    });

    it("should render Read More link", () => {
      render(<PostCard post={mockPost} lang="en" />);

      const link = screen.getByRole("link", { name: /read more/i });
      expect(link).toHaveAttribute("href", "/en/blog/test-post");
    });
  });

  describe("Date Formatting", () => {
    it("should format date in English locale", () => {
      render(<PostCard post={mockPost} lang="en" />);

      // Should show date in format like "Jan 15, 2024"
      expect(screen.getByText(/jan/i)).toBeInTheDocument();
      expect(screen.getByText(/2024/i)).toBeInTheDocument();
    });

    it("should format date in Arabic locale", () => {
      const arabicPost = { ...mockPost, lang: "ar" as const };

      render(<PostCard post={arabicPost} lang="ar" />);

      // Should format for Arabic locale
      expect(screen.getByText(/2024/)).toBeInTheDocument();
    });
  });

  describe("Bilingual Support", () => {
    it('should show "Read More" in English', () => {
      render(<PostCard post={mockPost} lang="en" />);

      expect(screen.getByText(/read more/i)).toBeInTheDocument();
    });

    it('should show "اقرأ المزيد" in Arabic', () => {
      const arabicPost = { ...mockPost, lang: "ar" as const };

      render(<PostCard post={arabicPost} lang="ar" />);

      expect(screen.getByText(/اقرأ المزيد/)).toBeInTheDocument();
    });

    it('should default to "Mawidi Team" for English when no author', () => {
      const postNoAuthor = { ...mockPost, author: "" };

      render(<PostCard post={postNoAuthor} lang="en" />);

      expect(screen.getByText("Mawidi Team")).toBeInTheDocument();
    });

    it('should default to "فريق موعدي" for Arabic when no author', () => {
      const postNoAuthor = { ...mockPost, author: "", lang: "ar" as const };

      render(<PostCard post={postNoAuthor} lang="ar" />);

      expect(screen.getByText("فريق موعدي")).toBeInTheDocument();
    });

    it("should set RTL direction for Arabic", () => {
      const arabicPost = { ...mockPost, lang: "ar" as const };
      const { container } = render(<PostCard post={arabicPost} lang="ar" />);

      const article = container.querySelector("article");
      expect(article).toHaveAttribute("dir", "rtl");
    });

    it("should set LTR direction for English", () => {
      const { container } = render(<PostCard post={mockPost} lang="en" />);

      const article = container.querySelector("article");
      expect(article).toHaveAttribute("dir", "ltr");
    });
  });

  describe("Image Handling", () => {
    it("should render cover image when provided", () => {
      render(<PostCard post={mockPost} lang="en" />);

      const image = screen.getByAltText("Test Blog Post");
      expect(image).toBeInTheDocument();
      expect(image).toHaveAttribute("src", expect.stringContaining("test.jpg"));
    });

    it("should render fallback gradient when no cover image", () => {
      const postNoCover = { ...mockPost, coverImage: undefined };
      const { container } = render(<PostCard post={postNoCover} lang="en" />);

      // Should render gradient div instead of image
      const gradient = container.querySelector(
        ".bg-gradient-to-br.from-brand-green",
      );
      expect(gradient).toBeInTheDocument();
    });

    it("should show tag icon in fallback image", () => {
      const postNoCover = { ...mockPost, coverImage: undefined };

      render(<PostCard post={postNoCover} lang="en" />);

      // Should show emoji for 'ai' tag
      expect(screen.getByText("🤖")).toBeInTheDocument();
    });
  });

  describe("Excerpt Cleaning", () => {
    it("should remove markdown symbols from excerpt", () => {
      const postWithMarkdown = {
        ...mockPost,
        excerpt: "**Bold** and _italic_ and `code` symbols",
      };

      render(<PostCard post={postWithMarkdown} lang="en" />);

      const excerpt = screen.getByText(/bold and italic and code/i);
      expect(excerpt.textContent).not.toContain("**");
      expect(excerpt.textContent).not.toContain("_");
      expect(excerpt.textContent).not.toContain("`");
    });

    it("should limit excerpt to 150 characters", () => {
      const longExcerpt = "A".repeat(200);
      const postLongExcerpt = { ...mockPost, excerpt: longExcerpt };

      render(<PostCard post={postLongExcerpt} lang="en" />);

      const excerptElement = screen.getByText(/A+\.\.\./);
      expect(excerptElement.textContent?.length).toBeLessThanOrEqual(154); // 150 + "..."
    });

    it("should normalize whitespace in excerpt", () => {
      const postMultiline = {
        ...mockPost,
        excerpt: "Line 1\n\n\nLine 2    with   spaces",
      };

      const { container } = render(<PostCard post={postMultiline} lang="en" />);
      const excerpt = container.querySelector(".line-clamp-3");

      expect(excerpt?.textContent).not.toContain("\n\n");
      expect(excerpt?.textContent).toMatch(/Line 1 Line 2 with spaces/);
    });
  });

  describe("Tag Icons", () => {
    it("should show payment icon for payments tag", () => {
      const postPayments = { ...mockPost, tags: ["payments"] };

      render(<PostCard post={postPayments} lang="en" />);

      expect(screen.getByText("💰")).toBeInTheDocument();
    });

    it("should show automation icon for automation tag", () => {
      const postAutomation = { ...mockPost, tags: ["automation"] };

      render(<PostCard post={postAutomation} lang="en" />);

      expect(screen.getByText("⚡")).toBeInTheDocument();
    });

    it("should show default icon for unknown tag", () => {
      const postUnknown = { ...mockPost, tags: ["unknown-tag"] };

      render(<PostCard post={postUnknown} lang="en" />);

      expect(screen.getByText("📝")).toBeInTheDocument();
    });

    it("should handle post with no tags", () => {
      const postNoTags = { ...mockPost, tags: [] };

      const { container } = render(<PostCard post={postNoTags} lang="en" />);

      // Should still render without errors
      expect(container.querySelector("article")).toBeInTheDocument();
    });
  });

  describe("Accessibility", () => {
    it("should have semantic article element", () => {
      const { container } = render(<PostCard post={mockPost} lang="en" />);

      expect(container.querySelector("article")).toBeInTheDocument();
    });

    it("should have accessible image alt text", () => {
      render(<PostCard post={mockPost} lang="en" />);

      const image = screen.getByAltText("Test Blog Post");
      expect(image).toBeInTheDocument();
    });

    it("should have clickable link to post", () => {
      render(<PostCard post={mockPost} lang="en" />);

      const link = screen.getByRole("link");
      expect(link).toHaveAttribute("href", "/en/blog/test-post");
    });
  });

  describe("Edge Cases", () => {
    it("should handle very long titles", () => {
      const longTitle = "A".repeat(200);
      const postLongTitle = { ...mockPost, title: longTitle };

      render(<PostCard post={postLongTitle} lang="en" />);

      // Should render with line-clamp
      const titleElement = screen.getByText(longTitle);
      expect(titleElement).toHaveClass("line-clamp-2");
    });

    it("should handle post without reading time", () => {
      const postNoTime = { ...mockPost, readingTime: undefined };

      const { container } = render(<PostCard post={postNoTime} lang="en" />);

      // Should render without reading time badge
      expect(container.querySelector("article")).toBeInTheDocument();
    });

    it("should handle empty excerpt", () => {
      const postNoExcerpt = { ...mockPost, excerpt: "" };

      render(<PostCard post={postNoExcerpt} lang="en" />);

      // Should still render article
      expect(screen.getByText("Test Blog Post")).toBeInTheDocument();
    });

    it("should handle special characters in author name", () => {
      const postSpecialAuthor = { ...mockPost, author: "O'Brien-Smith" };

      render(<PostCard post={postSpecialAuthor} lang="en" />);

      expect(screen.getByText("O'Brien-Smith")).toBeInTheDocument();
    });
  });
});
