import { useMemo } from "react";
import { MRT_ColumnDef } from "mantine-react-table";
import { AttributeSchema } from "../../../../../client";
import { IconCheck, IconX } from "@tabler/icons-react";
import { formatDate, formatDateTime } from "../../../../../types/utils.ts";
import { Box } from "@mantine/core";


const useAttributesTableColumns = () => {
    return useMemo<MRT_ColumnDef<AttributeSchema>[]>(
        () => [
            {
                header: "Название",
                accessorKey: "label",
            },
            {
                header: "Тип",
                accessorKey: "type.name",
            },
            {
                header: "Значение по умолчанию",
                accessorKey: "defaultValue",
                Cell: ({ cell, row }) => {
                    const value = cell.getValue();
                    if (value === null) return <>-</>;

                    const type = row.original.type.type;
                    if (type === "datetime") {
                        return formatDateTime(value as string);
                    }
                    if (type === "date") {
                        return formatDate(value as string);
                    }
                    if (type === "bool") {
                        return value ? <IconCheck /> : <IconX />;
                    }

                    return <>{value}</>;
                },
            },
            {
                header: "Синхронизировано в группе",
                accessorKey: "isApplicableToGroup",
                Cell: ({ cell }) => cell.getValue() ? (
                    <IconCheck />
                ) : (
                    <IconX />
                ),
            },
            {
                header: "Может быть пустым",
                accessorKey: "isNullable",
                Cell: ({ cell }) => cell.getValue() ? (
                    <IconCheck />
                ) : (
                    <IconX />
                ),
            },
            {
                header: "Описаниие",
                accessorKey: "description",
                Cell: ({ row }) => <Box>{row.original.description}</Box>,
            },
        ],
        [],
    );
};

export default useAttributesTableColumns;
