import { Dialog, Transition } from '@headlessui/react';
import { Fragment } from 'react';
import { Product } from '../types/api';
import { formatPrice } from '../../../utils/format-price';
import IconX from '../../../components/Icon/IconX';
import { formatDate } from '../../../utils/format-date';

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

export const DetailProductModal = ({ isOpen, onClose, product }: Props) => {
    if (!product) 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-4xl 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 Details
                                </Dialog.Title>

                                <div className="grid grid-cols-1 md:grid-cols-2 gap-6 text-[16px]">
                                    <Detail label="SKU" value={product.sku} />
                                    <Detail label="Name" value={product.name} />
                                    <Detail label="Category" value={product.category?.name} />
                                    <Detail label="Collection" value={product.collection?.name} />
                                    <Detail
                                        label="Colour"
                                        value={
                                            <div className="flex items-center gap-2">
                                                <span className="inline-block w-4 h-4 rounded-full border" style={{ backgroundColor: product.colour?.hex }} />
                                                {product.colour?.name}
                                            </div>
                                        }
                                    />
                                    <Detail label="Size" value={product.size?.name} />
                                    <Detail label="Price" value={formatPrice(product.price)} />
                                    <Detail label="HPP" value={formatPrice(product.hpp)} />
                                    <Detail label="Unit" value={product.unit_of_measure} />
                                    <Detail
                                        label="Raw Material"
                                        value={
                                            <span className={`px-2 py-1 rounded text-sm font-medium text-white ${product.is_raw_material ? 'bg-green-500' : 'bg-red-500'}`}>
                                                {product.is_raw_material ? 'Yes' : 'No'}
                                            </span>
                                        }
                                    />
                                    <Detail
                                        label="Finished Good"
                                        value={
                                            <span className={`px-2 py-1 rounded text-sm font-medium text-white ${product.is_finished_good ? 'bg-green-500' : 'bg-red-500'}`}>
                                                {product.is_finished_good ? 'Yes' : 'No'}
                                            </span>
                                        }
                                    />
                                    <Detail label="Created At" value={formatDate(product.CreatedAt)} />
                                </div>

                                {product.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">{product.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>
);
