"use client";

import Image from "next/image";

type Props = {
    productId: number;
    show: boolean;
    photosCount: number;
    qty: number;
    onIncreaseQty: () => void;
    onDecreaseQty: () => void;
    onOpenUpload: () => void;
    onAddToCart: () => void;
    addingToCart: boolean;
    isEditMode?: boolean;
};

export default function CalendarBottomBar({
    productId, show, photosCount, qty,
    onIncreaseQty, onDecreaseQty, onOpenUpload,
    onAddToCart, addingToCart, isEditMode = false,
}: Props) {
    if (!show) return null;

    const requiredPhotos = productId === 1 ? 7 : 12;
    const needsPhotos = photosCount !== requiredPhotos;
    const isDisabled = addingToCart || (productId !== 1 && needsPhotos);

    const buttonText = addingToCart
        ? (isEditMode ? "جاري حفظ التعديلات..." : "جاري الإضافة...")
        : (productId !== 1 && needsPhotos)
            ? "أضف 12 صورة للمتابعة"
            : isEditMode ? "حفظ التعديلات" : "أضف إلى السلة";

    return (
        <div className="fixed bottom-0 left-0 right-0 z-[80] flex justify-center px-4 pb-[max(12px,env(safe-area-inset-bottom))] pointer-events-none">
            <div className="pointer-events-auto w-full max-w-[390px] rounded-2xl bg-white shadow-[0_6px_30px_rgba(0,0,0,0.18)] border border-gray-100 p-3 content-ltr">
                <div className="flex items-center justify-between gap-2">
                    <button type="button" onClick={onOpenUpload} className="flex flex-col items-center text-pink-500 cursor-pointer shrink-0">
                        <span className="bg-pink-100 p-2 rounded-xl mb-1">
                            <Image src="/icons/plus.svg" alt="plus" width={22} height={22} />
                        </span>
                        <span className="text-xs">إضافة صور</span>
                    </button>
                    <div className="flex flex-col items-center text-gray-700">
                        <span className="text-xs mb-2">الكمية</span>
                        <div className="flex items-center gap-3">
                            <button type="button" onClick={onDecreaseQty} aria-label="Decrease quantity" className="w-8 h-8 rounded-full border border-gray-300 flex items-center justify-center">−</button>
                            <span className="min-w-6 text-center font-semibold">{qty}</span>
                            <button type="button" onClick={onIncreaseQty} aria-label="Increase quantity" className="w-8 h-8 rounded-full border border-gray-300 flex items-center justify-center">+</button>
                        </div>
                    </div>
                    <div className="flex flex-col items-center text-gray-600">
                        <div className="border border-[#F1F2F9] p-2 rounded-xl mb-1">
                            <span className="text-sm font-semibold">{photosCount}/{requiredPhotos}</span>
                        </div>
                        <span className="text-xs">الصور</span>
                    </div>
                </div>
                <button
                    type="button"
                    onClick={onAddToCart}
                    disabled={isDisabled}
                    className="mt-3 w-full h-[46px] rounded-xl bg-[#FF2B77] text-white font-bold transition hover:bg-[#ff176a] disabled:opacity-60 disabled:cursor-not-allowed"
                >
                    {buttonText}
                </button>
            </div>
        </div>
    );
}
