"use client";

import React, { FC, useState } from "react";
import { Drawer } from "@mantine/core";
import DealEditorBody from "@/app/deals/drawers/DealEditorDrawer/components/DealEditorBody";
import Header from "@/app/deals/drawers/DealEditorDrawer/components/Header";
import { DrawerProps } from "@/drawers/types";
import useIsMobile from "@/hooks/utils/useIsMobile";
import { DealSchema, ProjectSchema } from "@/lib/client";

type Props = {
    value: DealSchema;
    onChange: (deal: DealSchema) => void;
    onDelete: (deal: DealSchema, onSuccess: () => void) => void;
    project: ProjectSchema | null;
};

const DealEditorDrawer: FC<DrawerProps<Props>> = ({
    opened,
    onClose,
    props,
}) => {
    const isMobile = useIsMobile();
    const [deal, setDeal] = useState(props.value);

    return (
        <Drawer
            size={isMobile ? "100%" : "80%"}
            position={"right"}
            onClose={onClose}
            removeScrollProps={{ allowPinchZoom: true }}
            withCloseButton={isMobile}
            opened={opened}
            trapFocus={false}
            title={isMobile && <Header deal={deal} />}
            styles={{
                body: {
                    display: "flex",
                    flexDirection: "column",
                    padding: isMobile ? 0 : "var(--mantine-spacing-xs)",
                },
                header: {
                    paddingBlock: 0,
                },
            }}>
            <DealEditorBody
                value={deal}
                onChange={deal => {
                    setDeal(deal);
                    props.onChange(deal);
                }}
                onDelete={value => props.onDelete(value, onClose)}
                project={props.project}
            />
        </Drawer>
    );
};

export default DealEditorDrawer;
