"use client";

import { useState, ReactNode } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { ChevronDown, ChevronUp, RefreshCw, AlertCircle } from "lucide-react";

interface WidgetContainerProps {
    title: string;
    icon?: ReactNode;
    children: ReactNode;
    isLoading?: boolean;
    error?: string;
    onRefresh?: () => void;
    action?: ReactNode;
    skeleton?: ReactNode;
    collapsible?: boolean;
    className?: string;
}

export function WidgetContainer({
    title,
    icon,
    children,
    isLoading = false,
    error,
    onRefresh,
    action,
    skeleton,
    collapsible = true,
    className = ""
}: WidgetContainerProps) {
    const [isExpanded, setIsExpanded] = useState(true);

    return (
        <div className={`rounded-2xl border border-slate-200 bg-white shadow-sm flex flex-col overflow-hidden ${className}`}>
            {/* Header */}
            <div className="flex items-center justify-between p-4 sm:p-5 border-b border-slate-100 bg-white">
                <div className="flex items-center gap-2">
                    {icon && <div className="text-slate-500">{icon}</div>}
                    <h3 className="font-bold text-slate-900 md:text-lg select-none">{title}</h3>
                </div>

                <div className="flex items-center gap-1 sm:gap-2">
                    {action && <div className="mr-2">{action}</div>}

                    {onRefresh && (
                        <button
                            onClick={onRefresh}
                            className={`p-1.5 rounded-lg text-slate-400 hover:text-primary hover:bg-slate-50 transition-colors ${isLoading ? 'opacity-50 cursor-not-allowed' : ''}`}
                            disabled={isLoading}
                            title="Refresh Widget"
                        >
                            <RefreshCw className={`h-4 w-4 ${isLoading ? 'animate-spin text-primary' : ''}`} />
                        </button>
                    )}

                    {collapsible && (
                        <button
                            onClick={() => setIsExpanded(!isExpanded)}
                            className="p-1.5 rounded-lg text-slate-400 hover:text-slate-600 hover:bg-slate-50 transition-colors md:hidden"
                        >
                            {isExpanded ? <ChevronUp className="h-5 w-5" /> : <ChevronDown className="h-5 w-5" />}
                        </button>
                    )}
                </div>
            </div>

            {/* Content Body */}
            <AnimatePresence initial={false}>
                {isExpanded && (
                    <motion.div
                        initial={{ height: 0, opacity: 0 }}
                        animate={{ height: "auto", opacity: 1 }}
                        exit={{ height: 0, opacity: 0 }}
                        transition={{ duration: 0.3, ease: "easeInOut" }}
                    >
                        <div className="p-4 sm:p-5">
                            {error ? (
                                <div className="flex flex-col items-center justify-center p-6 text-center text-slate-500 bg-slate-50 rounded-xl">
                                    <AlertCircle className="h-8 w-8 text-rose-400 mb-2" />
                                    <p className="text-sm font-medium text-slate-700">{error}</p>
                                    {onRefresh && (
                                        <button onClick={onRefresh} className="mt-3 text-xs font-bold text-primary hover:underline">
                                            Try Again
                                        </button>
                                    )}
                                </div>
                            ) : isLoading && skeleton ? (
                                <div className="animate-pulse">{skeleton}</div>
                            ) : (
                                children
                            )}
                        </div>
                    </motion.div>
                )}
            </AnimatePresence>
        </div>
    );
}
