import { getPartnerBranding, PartnerBranding } from "../services/partnerBranding";

export interface PartnerDetails {
    partnerId: string;
    partnerName: string;
    partnerWebsite?: string;
    partnerLogoUrl?: string;

    style?: {
        backgroundColor?: string;
        footer?: {
            backgroundColor?: string;
            color?: string;
        };
    };
}

/**
 * Legacy hardcoded partner branding for fallback support
 */
const LEGACY_BRANDING: Record<string, PartnerDetails> = {
    td: {
        partnerId: "td",
        partnerName: "The Designery",
        partnerLogoUrl: "/partner/the-designery.png",
    },
    sfl: {
        partnerId: "sfl",
        partnerName: "Surefire Local",
        partnerWebsite: "https://www.surefirelocal.com/",
        partnerLogoUrl: "/partner/surefire-local.svg",
        style: {
            footer: {
                backgroundColor: "#fdfdfd",
                color: "#000000",
            },
        },
    },
    cg: {
        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)",
            },
        },
    },
    acm: {
        partnerId: "acm",
        partnerName: "All Contractor Marketing",
        partnerWebsite: "https://allcontractormarketing.com/",
        partnerLogoUrl: "/partner/all-contractor-marketing-wide.png",
        style: {
            backgroundColor: "#F5F5F5",
            footer: {
                backgroundColor: "#0B142E",
                color: "rgb(170, 170, 170)",
            },
        },
    },  
};

/**
 * Map legacy partner ID aliases to canonical IDs
 */
const PARTNER_ID_ALIASES: Record<string, string> = {
    "the-designery": "td",
    "surefire-local": "sfl",
    ca: "cg",
    "cost-guide": "cg",
    "contractor-appointments": "cg",
    'all-contractor-marketing': 'acm'
};

/**
 * Convert API branding response to PartnerDetails format
 */
function convertBrandingToDetails(branding: PartnerBranding): PartnerDetails {
    return {
        partnerId: branding.partnerCode,
        partnerName: branding.companyName,
        partnerWebsite: branding.companyWebsite || undefined,
        partnerLogoUrl: branding.logoUrl || undefined,
        style: branding.backgroundColor || branding.primaryColor
            ? {
                  backgroundColor: branding.backgroundColor || undefined,
                  footer: {
                      backgroundColor: branding.primaryColor || undefined,
                      color: branding.backgroundColor || undefined,
                  },
              }
            : undefined,
    };
}

/**
 * Get partner details based on the partner ID.
 * First tries to fetch from the dynamic API, then falls back to legacy hardcoded branding.
 *
 * @param partnerId - The partner ID or alias
 * @returns Promise resolving to partner details or undefined if not found
 */
export async function getPartnerDetailsAsync(
    partnerId?: string
): Promise<PartnerDetails | undefined> {
    if (!partnerId) {
        return undefined;
    }

    // Normalize partner ID using aliases
    const normalizedId = PARTNER_ID_ALIASES[partnerId] || partnerId;

    try {
        // Try fetching from API first
        const branding = await getPartnerBranding(normalizedId);
        if (branding) {
            return convertBrandingToDetails(branding);
        }

        // Fallback to legacy hardcoded branding
        return LEGACY_BRANDING[normalizedId];
    } catch (error) {
        console.error("Error fetching partner details:", error);
        // On error, fallback to legacy hardcoded branding
        return LEGACY_BRANDING[normalizedId];
    }
}

/**
 * Synchronous version of getPartnerDetails for backward compatibility.
 * Returns legacy hardcoded branding only.
 *
 * @deprecated Use getPartnerDetailsAsync instead for dynamic API support
 * @param partnerId - The partner ID or alias
 * @returns Partner details or undefined if not found
 */
export function getPartnerDetails(partnerId?: string): PartnerDetails | undefined {
    if (!partnerId) {
        return undefined;
    }

    // Normalize partner ID using aliases
    const normalizedId = PARTNER_ID_ALIASES[partnerId] || partnerId;

    return LEGACY_BRANDING[normalizedId];
}
