import { Box, Card, Group, Pill, Stack, Text } from "@mantine/core";
import { useDealsContext } from "@/app/deals/contexts/DealsContext";
import { useProjectsContext } from "@/app/deals/contexts/ProjectsContext";
import { useDrawersContext } from "@/drawers/DrawersContext";
import { DealSchema } from "@/lib/client";
import styles from "./DealCard.module.css";

type Props = {
    deal: DealSchema;
};

const DealCard = ({ deal }: Props) => {
    const { selectedProject } = useProjectsContext();
    const { dealsCrud } = useDealsContext();
    const { openDrawer } = useDrawersContext();

    const onClick = () => {
        openDrawer({
            key: "dealEditorDrawer",
            props: {
                value: deal,
                onChange: deal => dealsCrud.onUpdate(deal.id, deal),
                onDelete: dealsCrud.onDelete,
                project: selectedProject,
            },
        });
    };

    return (
        <Card
            onClick={onClick}
            className={styles.container}>
            <Group
                justify={"space-between"}
                wrap={"nowrap"}
                pl={"xs"}
                gap={"xs"}
                align={"start"}>
                <Text
                    c={"dodgerblue"}
                    mt={"xs"}>
                    {deal.name}
                </Text>
                <Box className={styles["deal-id"]}>
                    <Text style={{ textWrap: "nowrap" }}>ID: {deal.id}</Text>
                </Box>
            </Group>
            <Stack className={styles["deal-data"]}>
                <Stack gap={0}>
                    <Text>Wb электросталь</Text>
                    <Text>19 000 руб.</Text>
                    <Text>130 тов.</Text>
                </Stack>
                <Group gap={"xs"}>
                    <Pill className={styles["first-tag"]}>Срочно</Pill>
                    <Pill className={styles["second-tag"]}>Бесплатно</Pill>
                </Group>
            </Stack>
        </Card>
    );
};

export default DealCard;
