import { type ProjectSchemaWithCount } from "../../../../client";
import { ContextModalProps } from "@mantine/modals";
import { ActionIcon, Flex, rem, Stack, TextInput, Tooltip } from "@mantine/core";
import { BaseTable } from "../../../../components/BaseTable/BaseTable.tsx";
import useProjectsTableColumns from "./hooks/projectsTableColumns.tsx";
import { IconCheck, IconEdit, IconPlus, IconTrash } from "@tabler/icons-react";
import { MRT_TableOptions } from "mantine-react-table";
import InlineButton from "../../../../components/InlineButton/InlineButton.tsx";
import useProjectModal from "./hooks/useProjectModal.tsx";

type Props = {
    onUpdate: () => void;
};

const ProjectsModal = ({ innerProps }: ContextModalProps<Props>) => {
    const {
        projects,
        name,
        setName,
        editingProjects,
        handleEditClick,
        handleDeleteClick,
        handleCreateClick,
    } = useProjectModal(innerProps);

    const columns = useProjectsTableColumns({ editingProjects });

    return (
        <Stack gap={rem(10)}>
            <TextInput
                label={"Добавить проект"}
                variant={"default"}
                value={name}
                onChange={e => setName(e.target.value)}
            />
            <InlineButton
                variant={"default"}
                onClick={handleCreateClick}>
                <IconPlus />
                Добавить
            </InlineButton>

            <BaseTable
                data={projects}
                columns={columns}

                restProps={
                    {
                        enableSorting: false,
                        enableColumnActions: false,
                        enableRowActions: true,

                        renderRowActions: ({ row }) => (
                            <Flex gap="md">
                                <Tooltip label="Редактировать">
                                    <ActionIcon
                                        onClick={() => handleEditClick(row.original)}
                                        variant={"default"}>
                                        {
                                            editingProjects.has(row.original.id) ? (
                                                <IconCheck />
                                            ) : (
                                                <IconEdit />
                                            )
                                        }
                                    </ActionIcon>
                                </Tooltip>
                                <Tooltip label={"Удалить"}>
                                    <ActionIcon
                                        onClick={() => handleDeleteClick(row.original)}
                                        disabled={row.original.boardsCount > 0}
                                        variant={"default"}>
                                        <IconTrash />
                                    </ActionIcon>
                                </Tooltip>
                            </Flex>
                        ),
                    } as MRT_TableOptions<ProjectSchemaWithCount>
                }
            />
        </Stack>
    );
};

export default ProjectsModal;
