/**
 * @author: Hemal
 * File: getBlogDetails.test.ts
 * Purpose: Unit tests for the `getBlogDetails` API function.
 */

import getBlogDetails from "@/src/ssrapi/getBlogDetails";
import { API_ENDPOINTS } from "@/src/utils/commonVariables";
import { BlogResponse } from "@/src/types/api/blog.type";

describe("getBlogDetails", () => {
    const originalFetch = global.fetch;

    beforeAll(() => {
        // Set up environment variable for API base URL
        process.env.NEXT_PUBLIC_API_URL = "https://api.example.com";
    });

    beforeEach(() => {
        global.fetch = jest.fn();
    });

    afterEach(() => {
        jest.resetAllMocks();
    });

    afterAll(() => {
        global.fetch = originalFetch;
    });

    // Test case: fetch blog details with valid slug
    it("should fetch blog details for 'how-to-buy-a-home'", async () => {
        const mockSlug = "how-to-buy-a-home";
        const mockResponse: BlogResponse = {
          statusCode: 200,
          meta: {
            status: 1,
            message: "Blog details fetched successfully.",
          },
          data: {
            blogId: "123",
            blogImage: {
              imageId: "456",
              imageUrl: "https://example.com/image.jpg",
            },
            metaTitle: "How to Buy a Home",
            metaDescription: "Sample blog description",
            metaKeyWords: ["keyword1", "keyword2"],
            ogTitle: "How to Buy a Home",
            ogDescription: "Sample blog description",
            ogType: "article",
            ogImage: {
              imageId: "456",
              imageUrl: "https://example.com/image.jpg",
            },
            createdAt: "2023-08-15T12:34:56Z",
            description: "Sample blog description",
            readDuration: 5,
            shortDescription: "Sample blog short description",
            slug: "how-to-buy-a-home",
            status: 1,
            title: "How to Buy a Home",
            updatedAt: "2023-08-15T12:34:56Z",
          },
        };

        (global.fetch as jest.Mock).mockResolvedValueOnce({
            json: jest.fn().mockResolvedValueOnce(mockResponse)
        });

        const response = await getBlogDetails(mockSlug);

        expect(global.fetch).toHaveBeenCalledWith(
            `${process.env.NEXT_PUBLIC_API_URL}${API_ENDPOINTS.BLOG_DETAILS}`,
            {
                method: "POST",
                body: JSON.stringify({ slug: mockSlug }),
                headers: {
                    "Content-Type": "application/json",
                    "usertype": "guest"  // <-- ensure this matches the implementation
                },
                cache: "no-store"
            }
        );

        expect(response).toEqual(mockResponse);
    });

    // Test case: return undefined on fetch error
    it("should return undefined on fetch error", async () => {
        const slug = "how-to-buy-a-home";

        (global.fetch as jest.Mock).mockRejectedValueOnce(new Error("Network error"));

        const response = await getBlogDetails(slug);

        expect(response).toBeUndefined();
    });

    // Test case: invalid slug returns undefined
    it("should return undefined for invalid blog slug", async () => {
        const invalidSlug = "non-existent-blog";

        (global.fetch as jest.Mock).mockResolvedValueOnce({
            json: jest.fn().mockResolvedValueOnce(undefined)
        });

        const response = await getBlogDetails(invalidSlug);

        expect(response).toBeUndefined();
    });
});
