import { DealGroupSchema, DealService, DealSummary } from "../../../client";
import { FC, useEffect, useMemo, useState } from "react";
import DealSummaryCard from "../DealSummaryCard/DealSummaryCard.tsx";
import { Flex, rem, Text, TextInput, useMantineColorScheme } from "@mantine/core";
import { IconGripHorizontal } from "@tabler/icons-react";
import { useDebouncedValue } from "@mantine/hooks";
import { notifications } from "../../../shared/lib/notifications.ts";

type Props = {
    deals: DealSummary[];
    group: DealGroupSchema;
}

export const DealGroupView: FC<Props> = ({ deals, group }) => {
    const theme = useMantineColorScheme();
    const [name, setName] = useState<string>(group.name || "");
    const [debouncedName] = useDebouncedValue(name, 200);
    const totalPrice = useMemo(() => deals.reduce((acc, deal) => acc + deal.totalPrice, 0), [deals]);
    const totalProducts = useMemo(() => deals.reduce((acc, deal) => acc + deal.totalProducts, 0), [deals]);
    const updateName = () => {
        if (debouncedName === group.name) return;
        DealService.updateDealGroup({
            requestBody: {
                data: {
                    ...group,
                    name: debouncedName,
                },
            },
        }).then(response => {
            if (response.ok) return;
            setName(group.name || "");
            notifications.guess(response.ok, { message: response.message });
        });
    };

    useEffect(() => {
        updateName();
    }, [debouncedName]);
    return (
        <Flex
            style={{
                border: "dashed var(--item-border-size) var(--mantine-color-default-border)",
                borderRadius: "0.5rem",
            }}

            p={rem(5)}
            py={rem(10)}
            bg={theme.colorScheme === "dark" ? "var(--mantine-color-dark-5)" : "var(--mantine-color-gray-1)"}
            gap={rem(10)}
            direction={"column"}>
            <Flex
                justify={"space-between"}
                align={"center"}
                gap={rem(10)}
                px={rem(10)}
            >
                <TextInput
                    value={name}
                    onChange={event => setName(event.currentTarget.value)}
                    variant={"unstyled"}
                />
                <IconGripHorizontal />
            </Flex>
            <Flex direction={"column"} gap={rem(10)}>
                {deals.map(deal => (
                    <DealSummaryCard
                        color={theme.colorScheme === "dark" ? "var(--mantine-color-dark-6)" : "var(--mantine-color-gray-2)"}
                        key={deal.id}
                        dealSummary={deal}
                    />
                ))}
            </Flex>
            <Flex
                p={rem(10)}
                direction={"column"}
                bg={theme.colorScheme === "dark" ? "var(--mantine-color-dark-6)" : "var(--mantine-color-gray-2)"}
                style={{ borderRadius: "0.5rem" }}
            >
                <Text
                    c={"gray.6"}
                    size={"xs"}>Сумма: {totalPrice.toLocaleString("ru-RU")} руб.</Text>
                <Text
                    c={"gray.6"}
                    size={"xs"}>Всего товаров: {totalProducts.toLocaleString("ru-RU")} шт.</Text>
            </Flex>
        </Flex>
    );
};