import { cn } from '@/lib/utils';
import { SharedContentLayout } from '@/components/SharedContentLayout';
import { getSectionBackground } from '@/lib/sectionStyles';

interface ImageTextSectionProps {
    id: string;
    layout?: string;
    colorScheme?: string;
    title?: string;
    text?: string;
    image?: string;
    elementStyles?: Record<string, any>;
}

export function ImageTextSection({
    id,
    layout = 'image-left',
    colorScheme = 'default',
    title,
    text,
    image,
    cta_text,
    cta_url,
    elementStyles,
    styles,
}: ImageTextSectionProps & { styles?: Record<string, any>, cta_text?: string, cta_url?: string }) {
    const isImageRight = layout === 'image-right' || layout === 'right';

    // Helper to get text styles (including font family)
    const getTextStyles = (elementName: string) => {
        const styles = elementStyles?.[elementName] || {};
        return {
            classNames: cn(
                styles.fontSize,
                styles.fontWeight,
                {
                    'font-sans': styles.fontFamily === 'secondary',
                    'font-serif': styles.fontFamily === 'primary',
                }
            ),
            style: {
                color: styles.color,
                textAlign: styles.textAlign,
                backgroundColor: styles.backgroundColor,
                borderColor: styles.borderColor,
                borderWidth: styles.borderWidth,
                borderRadius: styles.borderRadius,
            }
        };
    };

    const titleStyle = getTextStyles('title');
    const textStyle = getTextStyles('text');
    const buttonStyle = getTextStyles('button');

    const imageStyle = elementStyles?.['image'] || {};

    // Height preset support
    const heightPreset = styles?.heightPreset || 'default';
    const heightMap: Record<string, string> = {
        'default': 'py-12 md:py-24',
        'small': 'py-8 md:py-16',
        'medium': 'py-16 md:py-32',
        'large': 'py-24 md:py-48',
        'screen': 'min-h-screen py-20 flex items-center',
    };
    const heightClasses = heightMap[heightPreset] || 'py-12 md:py-24';

    const hasCustomBackground = !!styles?.backgroundColor || !!styles?.backgroundImage || styles?.backgroundType === 'gradient';
    const sectionBg = getSectionBackground(styles);

    // Helper to get background style for dynamic schemes
    const getBackgroundStyle = (): React.CSSProperties | undefined => {
        if (hasCustomBackground) return sectionBg.style;
        if (colorScheme === 'primary') {
            return { backgroundColor: 'var(--wn-primary, #1a1a1a)' };
        }
        if (colorScheme === 'secondary') {
            return { backgroundColor: 'var(--wn-secondary, #6b7280)' };
        }
        return undefined;
    };

    return (
        <section
            id={id}
            className={cn(
                'wn-section wn-image-text',
                `wn-scheme--${colorScheme}`,
                !styles?.paddingTop && !styles?.paddingBottom && heightClasses,
                {
                    'bg-muted': colorScheme === 'muted' && !hasCustomBackground,
                }
            )}
            style={getBackgroundStyle()}
        >
            <SharedContentLayout
                title={title}
                text={text}
                image={image}
                imagePosition={isImageRight ? 'right' : 'left'}
                containerWidth={styles?.contentWidth === 'full' ? 'full' : styles?.contentWidth === 'boxed' ? 'boxed' : 'contained'}
                titleStyle={titleStyle.style}
                titleClassName={titleStyle.classNames}
                textStyle={textStyle.style}
                textClassName={textStyle.classNames}
                imageStyle={{
                    backgroundColor: imageStyle.backgroundColor,
                    objectFit: imageStyle.objectFit,
                }}
                buttons={cta_text && cta_url ? [{ text: cta_text, url: cta_url }] : []}
                buttonStyle={{
                    classNames: buttonStyle.classNames,
                    style: buttonStyle.style
                }}
            />
        </section>
    );
}
