import { Dialog, Transition } from '@headlessui/react';
import { Fragment } from 'react';
import { ProductCollection } from '../types/api';
import IconX from '../../../components/Icon/IconX';

interface Props {
    isOpen: boolean;
    onClose: () => void;
    collection: ProductCollection | null;
}

export const DetailProductCollectionModal = ({ isOpen, onClose, collection }: Props) => {
    if (!collection) return null;

    return (
        <Transition appear show={isOpen} as={Fragment}>
            <Dialog as="div" onClose={onClose} className="relative z-[60]">
                <Transition.Child as={Fragment} enter="ease-out duration-300" enterFrom="opacity-0" enterTo="opacity-100" leave="ease-in duration-200" leaveFrom="opacity-100" leaveTo="opacity-0">
                    <div className="fixed inset-0 bg-black/50" />
                </Transition.Child>

                <div className="fixed inset-0 overflow-y-auto">
                    <div className="flex min-h-full items-center justify-center p-4">
                        <Transition.Child
                            as={Fragment}
                            enter="ease-out duration-300"
                            enterFrom="opacity-0 scale-95"
                            enterTo="opacity-100 scale-100"
                            leave="ease-in duration-200"
                            leaveFrom="opacity-100 scale-100"
                            leaveTo="opacity-0 scale-95"
                        >
                            <Dialog.Panel className="w-full max-w-2xl transform overflow-hidden rounded-xl bg-white dark:bg-gray-900 p-6 text-left align-middle shadow-xl transition-all relative">
                                <button type="button" className="absolute top-4 right-4 text-gray-400 hover:text-gray-600" onClick={onClose}>
                                    <IconX />
                                </button>

                                <Dialog.Title as="h3" className="text-2xl font-bold mb-6">
                                    Product Collection Details
                                </Dialog.Title>

                                <div className="grid grid-cols-1 sm:grid-cols-2 gap-6 text-[16px]">
                                    <Detail label="Name" value={collection.name} />
                                    <Detail label="Code" value={collection.code} />
                                </div>

                                {collection.description && (
                                    <div className="mt-8">
                                        <h4 className="text-lg font-semibold mb-1">Description</h4>
                                        <p className="text-base text-gray-700 dark:text-gray-300">{collection.description}</p>
                                    </div>
                                )}
                            </Dialog.Panel>
                        </Transition.Child>
                    </div>
                </div>
            </Dialog>
        </Transition>
    );
};

const Detail = ({ label, value }: { label: string; value: React.ReactNode }) => (
    <div className="flex flex-col">
        <span className="text-gray-500 dark:text-gray-400 text-base font-medium">{label}:</span>
        <span className="text-lg font-semibold text-black dark:text-white mt-1">{value}</span>
    </div>
);
