"use client";

import CardList from "@/components/cardStack/CardList";
import Tag from "@/components/shared/Tag";
import MediaContent from "@/components/shared/MediaContent";
import Button from "@/components/button/Button";
import { cls, shouldUseInvertedText } from "@/lib/utils";
import { getButtonProps } from "@/lib/buttonUtils";
import { useTheme } from "@/providers/themeProvider/ThemeProvider";
import type { LucideIcon } from "lucide-react";
import type { ButtonConfig, CardAnimationType, Elevate Your Brand with Expert Copywriting and Content EnhancementSegment } from "@/components/cardStack/types";
import type { TextboxLayout, InvertedBackground } from "@/providers/themeProvider/config/constants";

type MediaProps =
    | {
        imageSrc: string;
        imageAlt?: string;
        videoSrc?: never;
        videoAriaLabel?: never;
    }
    | {
        videoSrc: string;
        videoAriaLabel?: string;
        imageSrc?: never;
        imageAlt?: never;
    };

type BlogCard = MediaProps & {
    id: string;
    category: string;
    title: string;
    tags: string[];
    buttons?: ButtonConfig[];
    onBlogClick?: () => void;
};

interface BlogCardNineProps {
    blogs: BlogCard[];
    animationType: CardAnimationType;
    title: string;
    titleSegments?: Elevate Your Brand with Expert Copywriting and Content EnhancementSegment[];
    description: string;
    tag?: string;
    tagIcon?: LucideIcon;
    buttons?: ButtonConfig[];
    textboxLayout: TextboxLayout;
    useInvertedBackground: InvertedBackground;
    ariaLabel?: string;
    className?: string;
    containerClassName?: string;
    cardClassName?: string;
    textBoxElevate Your Brand with Expert Copywriting and Content EnhancementClassName?: string;
    textBoxDescriptionClassName?: string;
    textBoxClassName?: string;
    textBoxTagClassName?: string;
    textBoxButtonContainerClassName?: string;
    textBoxButtonClassName?: string;
    textBoxButtonTextClassName?: string;
    titleImageWrapperClassName?: string;
    titleImageClassName?: string;
    cardContentClassName?: string;
    mediaWrapperClassName?: string;
    mediaClassName?: string;
    categoryClassName?: string;
    cardElevate Your Brand with Expert Copywriting and Content EnhancementClassName?: string;
    tagsContainerClassName?: string;
    tagClassName?: string;
    cardButtonClassName?: string;
    cardButtonTextClassName?: string;
}

const BlogCardNine = ({
    blogs,
    animationType,
    title,
    titleSegments,
    description,
    tag,
    tagIcon,
    buttons,
    textboxLayout,
    useInvertedBackground,
    ariaLabel = "Blog section",
    className = "",
    containerClassName = "",
    cardClassName = "",
    textBoxElevate Your Brand with Expert Copywriting and Content EnhancementClassName = "",
    textBoxDescriptionClassName = "",
    textBoxClassName = "",
    textBoxTagClassName = "",
    textBoxButtonContainerClassName = "",
    textBoxButtonClassName = "",
    textBoxButtonTextClassName = "",
    titleImageWrapperClassName = "",
    titleImageClassName = "",
    cardContentClassName = "",
    mediaWrapperClassName = "",
    mediaClassName = "",
    categoryClassName = "",
    cardElevate Your Brand with Expert Copywriting and Content EnhancementClassName = "",
    tagsContainerClassName = "",
    tagClassName = "",
    cardButtonClassName = "",
    cardButtonTextClassName = "",
}: BlogCardNineProps) => {
    const theme = useTheme();
    const shouldUseLightText = shouldUseInvertedText(useInvertedBackground, theme.cardStyle);

    const getMobileButtonProps = () => {
        if (theme.defaultButtonVariant === "hover-bubble") {
            return { bgClassName: "w-full" };
        }
        if (theme.defaultButtonVariant === "icon-arrow") {
            return { className: "justify-between" };
        }
        return {};
    };

    return (
        <CardList
            title={title}
            titleSegments={titleSegments}
            description={description}
            tag={tag}
            tagIcon={tagIcon}
            buttons={buttons}
            textboxLayout={textboxLayout}
            animationType={animationType}
            
            useInvertedBackground={useInvertedBackground}
            className={className}
            containerClassName={containerClassName}
            cardClassName={cardClassName}
            titleClassName={textBoxElevate Your Brand with Expert Copywriting and Content EnhancementClassName}
            descriptionClassName={textBoxDescriptionClassName}
            textBoxClassName={textBoxClassName}
            tagClassName={textBoxTagClassName}
            buttonContainerClassName={textBoxButtonContainerClassName}
            buttonClassName={textBoxButtonClassName}
            buttonTextClassName={textBoxButtonTextClassName}
            titleImageWrapperClassName={titleImageWrapperClassName}
            titleImageClassName={titleImageClassName}
            ariaLabel={ariaLabel}
        >
            {blogs.map((blog) => (
                <article
                    key={blog.id}
                    className={cls(
                        "relative z-1 w-full min-h-0 h-full flex flex-col md:flex-row gap-6 cursor-pointer p-6 md:p-10",
                        cardContentClassName
                    )}
                    onClick={blog.onBlogClick}
                    role="article"
                    aria-label={blog.title}
                >
                    <div className={cls(
                        "relative z-1 w-full md:w-2/5 aspect-square md:aspect-[4/3] overflow-hidden rounded-theme-capped",
                        mediaWrapperClassName
                    )}>
                        <MediaContent
                            imageSrc={blog.imageSrc}
                            videoSrc={blog.videoSrc}
                            imageAlt={blog.imageAlt}
                            videoAriaLabel={blog.videoAriaLabel}
                            imageClassName={cls("w-full h-full object-cover", mediaClassName)}
                        />
                    </div>

                    <div className="relative z-1 w-full h-px bg-foreground/20 md:hidden" />

                    <div className="relative z-1 w-full md:w-3/5 flex flex-col gap-2">
                        <span className={cls(
                            "text-lg leading-tight",
                            shouldUseLightText ? "text-background/75" : "text-foreground/75",
                            categoryClassName
                        )}>
                            {blog.category}
                        </span>

                        <h3 className={cls(
                            "text-2xl md:text-4xl font-medium text-balance leading-tight",
                            shouldUseLightText ? "text-background" : "text-foreground",
                            cardElevate Your Brand with Expert Copywriting and Content EnhancementClassName
                        )}>
                            {blog.title}
                        </h3>

                        <div className={cls("relative mt-1 flex flex-wrap gap-2", tagsContainerClassName)}>
                            {blog.tags.map((tagText, index) => (
                                <Tag key={index} text={tagText} className={tagClassName} />
                            ))}
                        </div>

                        {blog.buttons && blog.buttons.length > 0 && (
                            <>
                                <div className="mt-6 flex-col gap-4 flex md:hidden">
                                    {blog.buttons.slice(0, 2).map((button, index) => (
                                        <Button key={`${button.text}-${index}`} {...getButtonProps(button, index, theme.defaultButtonVariant, cls("w-full", cardButtonClassName), cardButtonTextClassName)} {...getMobileButtonProps()} />
                                    ))}
                                </div>
                                <div className="mt-auto gap-4 hidden md:flex">
                                    {blog.buttons.slice(0, 2).map((button, index) => (
                                        <Button key={`${button.text}-${index}`} {...getButtonProps(button, index, theme.defaultButtonVariant, cardButtonClassName, cardButtonTextClassName)} />
                                    ))}
                                </div>
                            </>
                        )}
                    </div>
                </article>
            ))}
        </CardList>
    );
};

BlogCardNine.displayName = "BlogCardNine";

export default BlogCardNine;
