/**
 * Unit tests for partner details utilities
 */
import { getPartnerDetails, getPartnerDetailsAsync } from "./partnerDetails";
import * as partnerBrandingService from "../services/partnerBranding";

// Mock the partner branding service
jest.mock("../services/partnerBranding");

describe("partnerDetails", () => {
    describe("getPartnerDetails (synchronous)", () => {
        it("should return details for 'sfl' partner", () => {
            const result = getPartnerDetails("sfl");

            expect(result).toEqual({
                partnerId: "sfl",
                partnerName: "Surefire Local",
                partnerWebsite: "https://www.surefirelocal.com/",
                partnerLogoUrl: "/partner/surefire-local.svg",
                style: {
                    footer: {
                        backgroundColor: "#fdfdfd",
                        color: "#000000",
                    },
                },
            });
        });

        it("should return details for 'td' partner", () => {
            const result = getPartnerDetails("td");

            expect(result).toEqual({
                partnerId: "td",
                partnerName: "The Designery",
                partnerLogoUrl: "/partner/the-designery.png",
            });
        });

        it("should return details for 'cg' partner", () => {
            const result = getPartnerDetails("cg");

            expect(result).toEqual({
                partnerId: "cg",
                partnerName: "Cost Guide",
                partnerWebsite: "https://www.costguide.com/",
                partnerLogoUrl: "/partner/cost-guide-logo.png",
                style: {
                    backgroundColor: "#ffffff",
                    footer: {
                        backgroundColor: "#ffffff",
                        color: "rgb(170, 170, 170)",
                    },
                },
            });
        });

        it("should handle partner ID aliases", () => {
            expect(getPartnerDetails("surefire-local")).toEqual(getPartnerDetails("sfl"));
            expect(getPartnerDetails("the-designery")).toEqual(getPartnerDetails("td"));
            expect(getPartnerDetails("ca")).toEqual(getPartnerDetails("cg"));
            expect(getPartnerDetails("cost-guide")).toEqual(getPartnerDetails("cg"));
            expect(getPartnerDetails("contractor-appointments")).toEqual(getPartnerDetails("cg"));
        });

        it("should return undefined for unknown partner", () => {
            const result = getPartnerDetails("unknown");

            expect(result).toBeUndefined();
        });

        it("should return undefined when partnerId is undefined", () => {
            const result = getPartnerDetails(undefined);

            expect(result).toBeUndefined();
        });
    });

    describe("getPartnerDetailsAsync (with API)", () => {
        beforeEach(() => {
            jest.clearAllMocks();
        });

        it("should fetch from API and return converted details", async () => {
            const mockBranding = {
                partnerCode: "new-partner",
                logoUrl: "https://example.com/logo.png",
                primaryColor: "#1a5f7a",
                backgroundColor: "#f5f5f5",
                companyName: "New Partner Inc",
                companyWebsite: "https://newpartner.com",
            };

            (partnerBrandingService.getPartnerBranding as jest.Mock).mockResolvedValue(
                mockBranding
            );

            const result = await getPartnerDetailsAsync("new-partner");

            expect(result).toEqual({
                partnerId: "new-partner",
                partnerName: "New Partner Inc",
                partnerWebsite: "https://newpartner.com",
                partnerLogoUrl: "https://example.com/logo.png",
                style: {
                    backgroundColor: "#f5f5f5",
                    footer: {
                        backgroundColor: "#f5f5f5",
                        color: "#1a5f7a",
                    },
                },
            });
        });

        it("should fallback to legacy branding when API returns null", async () => {
            (partnerBrandingService.getPartnerBranding as jest.Mock).mockResolvedValue(null);

            const result = await getPartnerDetailsAsync("sfl");

            expect(result).toEqual({
                partnerId: "sfl",
                partnerName: "Surefire Local",
                partnerWebsite: "https://www.surefirelocal.com/",
                partnerLogoUrl: "/partner/surefire-local.svg",
                style: {
                    footer: {
                        backgroundColor: "#fdfdfd",
                        color: "#000000",
                    },
                },
            });
        });

        it("should fallback to legacy branding when API throws error", async () => {
            (partnerBrandingService.getPartnerBranding as jest.Mock).mockRejectedValue(
                new Error("Network error")
            );

            const result = await getPartnerDetailsAsync("sfl");

            expect(result).toEqual({
                partnerId: "sfl",
                partnerName: "Surefire Local",
                partnerWebsite: "https://www.surefirelocal.com/",
                partnerLogoUrl: "/partner/surefire-local.svg",
                style: {
                    footer: {
                        backgroundColor: "#fdfdfd",
                        color: "#000000",
                    },
                },
            });
        });

        it("should normalize partner ID using aliases", async () => {
            (partnerBrandingService.getPartnerBranding as jest.Mock).mockResolvedValue(null);

            await getPartnerDetailsAsync("surefire-local");

            // Should have called with normalized ID
            expect(partnerBrandingService.getPartnerBranding).toHaveBeenCalledWith("sfl");
        });

        it("should return undefined when partnerId is undefined", async () => {
            const result = await getPartnerDetailsAsync(undefined);

            expect(result).toBeUndefined();
            expect(partnerBrandingService.getPartnerBranding).not.toHaveBeenCalled();
        });

        it("should return undefined for unknown partner with no legacy fallback", async () => {
            (partnerBrandingService.getPartnerBranding as jest.Mock).mockResolvedValue(null);

            const result = await getPartnerDetailsAsync("unknown-partner");

            expect(result).toBeUndefined();
        });

        it("should handle API response without backgroundColor", async () => {
            const mockBranding = {
                partnerCode: "minimal-partner",
                logoUrl: "https://example.com/logo.png",
                primaryColor: "#1a5f7a",
                backgroundColor: "",
                companyName: "Minimal Partner",
                companyWebsite: "https://minimal.com",
            };

            (partnerBrandingService.getPartnerBranding as jest.Mock).mockResolvedValue(
                mockBranding
            );

            const result = await getPartnerDetailsAsync("minimal-partner");

            expect(result?.style).toBeUndefined();
        });
    });
});
