/**
 * WhatsApp Widget (Live)
 * 
 * Quick connect to WhatsApp with support for:
 * - Single contact (shows button directly)
 * - Multiple contacts (shows list to choose from)
 * - Custom message templates
 * 
 * Uses the shared WhatsApp component for parity with preview.
 * 
 * @since 1.0.0
 * @updated 1.6.0 - Migrated to shared component
 */

import { WhatsApp } from '@shared/components/widgets/WhatsApp';
import type { GlobalValues, FieldValue } from '@/utils/resolveAppearance';
import type { ButtonStyleSettings } from '@/types';

interface WhatsAppContact {
    name: string;
    phone: string;
    title?: string;
    avatar?: string;
}

// Must match the structure from admin/src/types/appearance.ts
interface WhatsAppWidgetSettings {
    button?: {
        background?: FieldValue;
        color?: FieldValue;
        borderRadius?: FieldValue;
        padding?: FieldValue;
        shadow?: FieldValue;
        hoverBackground?: FieldValue;
        buttonStyle?: ButtonStyleSettings;
    };
    icon?: {
        size?: FieldValue;
        color?: FieldValue;
    };
    text?: {
        color?: FieldValue;
        font?: FieldValue;
        typography?: Record<string, FieldValue>;
    };
}

interface WhatsAppWidgetProps {
    config: {
        contacts: WhatsAppContact[];
        message_template?: string;
        show_all_contacts?: boolean;
        is_rtl?: boolean;
    };
    widgetSettings?: WhatsAppWidgetSettings;
    globalValues?: GlobalValues;
    language?: string;
    context?: {
        page_url?: string;
        conversation_id?: string;
    };
}

export function WhatsAppWidget({ config, widgetSettings, globalValues = {}, language, context }: WhatsAppWidgetProps) {
    const { 
        contacts, 
        message_template, 
        show_all_contacts = true,
        is_rtl = true 
    } = config;

    return (
        <WhatsApp
            globalValues={globalValues}
            widgetSettings={widgetSettings as any}
            context="live"
            isRTL={is_rtl}
            language={language}
            contacts={contacts}
            messageTemplate={message_template}
            showAllContacts={show_all_contacts}
            pageUrl={context?.page_url}
        />
    );
}

export default WhatsAppWidget;
