import { FC } from "react";
import { IconCopy, IconMoodSad } from "@tabler/icons-react";
import { Button, Flex, Group, Stack, Text } from "@mantine/core";
import ActionIconWithTip from "@/components/ui/ActionIconWithTip/ActionIconWithTip";
import BaseTable from "@/components/ui/BaseTable/BaseTable";
import { DealProductSchema } from "@/lib/client";
import useProductServicesTable from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useProductServicesTable";
import useProductServicesTableColumns from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/hooks/utils/useProductServicesTableColumns";

type Props = {
    dealProduct: DealProductSchema;
    onDuplicateServices?: () => void;
    onKitAdd?: () => void;
};

const ProductServicesTable: FC<Props> = ({
    dealProduct,
    onDuplicateServices,
    onKitAdd,
}) => {
    const {
        onChange,
        onCreate,
        onDelete,
        isEmptyTable,
        isDuplicateServicesDisabled,
    } = useProductServicesTable({
        dealProduct,
    });

    const columns = useProductServicesTableColumns({
        data: dealProduct.productServices,
        quantity: dealProduct.quantity,
        onDelete,
        onChange,
    });

    return (
        <Stack gap={0}>
            <BaseTable
                records={dealProduct.productServices}
                columns={columns}
                groups={undefined}
                idAccessor={"serviceId"}
                withTableBorder
                style={{
                    height: isEmptyTable ? "8rem" : "auto",
                }}
                emptyState={
                    <Group
                        gap={"xs"}
                        mt={isEmptyTable ? "xl" : 0}>
                        <Text>Нет услуг</Text>
                        <IconMoodSad />
                    </Group>
                }
            />
            <Flex
                justify={"flex-end"}
                gap={"xs"}
                pt={"xs"}>
                {onDuplicateServices && (
                    <ActionIconWithTip
                        tipLabel={"Продублировать услуги"}
                        disabled={isDuplicateServicesDisabled}
                        onClick={onDuplicateServices}>
                        <IconCopy />
                    </ActionIconWithTip>
                )}
                {onKitAdd && (
                    <Button
                        onClick={onKitAdd}
                        variant={"default"}>
                        Добавить набор услуг
                    </Button>
                )}
                <Button
                    onClick={onCreate}
                    variant={"default"}>
                    Добавить услугу
                </Button>
            </Flex>
        </Stack>
    );
};
export default ProductServicesTable;
