"use client";
import DataTableWithAuth from "@/core/components/DataTableWithAuth";
import { GET_MACHINES_LIST } from "@/core/utils/routes";
import { Box, Typography } from "@mui/material";
import { useMemo } from "react";
import RowActions from "./RowActions";

const statusOptions = [
    { value: "", label: "همه وضعیت ها" },
    { value: 1, label: "آماده" },
    { value: 2, label: "رزرو" },
    { value: 3, label: "در ماموریت" },
    { value: 4, label: "خارج از رده" },
    { value: 5, label: "در تعمیر" },
    { value: 6, label: "فروخته شده" },
    { value: 7, label: "نیاز به تعمیر" },
];

const MachinesList = () => {
    const columns = useMemo(() => {
        return [
            {
                accessorKey: "id",
                header: "کد یکتا", // Unique Code
                id: "id",
                enableColumnFilter: true,
                datatype: "text",
                filterMode: "equals",
                sortDescFirst: false,
                columnFilterModeOptions: ["equals", "contains"],
                grow: false,
                size: 50,
            },

            {
                header: "اطلاعات ماشین‌ها",
                id: "machines_info",
                enableColumnFilter: false,
                enableSorting: false,
                datatype: "text",
                grow: false,
                columns: [
                    {
                        accessorKey: "car_name",
                        header: "نام ماشین",
                        id: "car_name",
                        enableColumnFilter: true,
                        datatype: "text",
                        filterMode: "equals",
                        sortDescFirst: false,
                        columnFilterModeOptions: ["equals", "contains"],
                        grow: false,
                        size: 100,
                    },
                    {
                        accessorKey: "car_type",
                        header: "نوع ماشین",
                        id: "car_type",
                        enableColumnFilter: true,
                        datatype: "text",
                        filterMode: "equals",
                        sortDescFirst: false,
                        columnFilterModeOptions: ["equals", "contains"],
                        grow: false,
                        size: 100,
                    },
                    {
                        accessorKey: "machine_code",
                        header: "کد ماشین",
                        id: "machine_code",
                        enableColumnFilter: true,
                        datatype: "text",
                        filterMode: "equals",
                        sortDescFirst: false,
                        columnFilterModeOptions: ["equals", "contains"],
                        grow: false,
                        size: 100,
                    },
                    {
                        accessorKey: "plak_number",
                        header: "پلاک",
                        id: "plak_number",
                        enableColumnFilter: true,
                        datatype: "text",
                        filterMode: "equals",
                        sortDescFirst: false,
                        columnFilterModeOptions: ["equals", "contains"],
                        grow: false,
                        size: 100,
                    },
                    {
                        accessorKey: "status",
                        header: "وضعیت",
                        id: "status",
                        enableColumnFilter: true,
                        datatype: "numeric",
                        filterMode: "equals",
                        grow: false,
                        size: 100,
                        columnSelectOption: () => {
                            return statusOptions.map((status) => ({
                                value: status.value,
                                label: status.label,
                            }));
                        },
                        Cell: ({ row }) => {
                            const status = statusOptions.find((option) => option.value == row.original.status);
                            return <Typography variant="body2">{status?.label || ""}</Typography>;
                        },
                    },
                ],
            },
        ];
    }, []);

    return (
        <>
            <Box sx={{ p: 1, border: 1, borderColor: "divider", borderRadius: 1 }}>
                <DataTableWithAuth
                    need_filter={true}
                    columns={columns}
                    table_url={GET_MACHINES_LIST}
                    page_name={"machines"}
                    table_name={"machinesList"}
                    sorting={[{ id: "id", desc: true }]}
                    enableRowActions
                    positionActionsColumn={"first"}
                    RowActions={RowActions}
                />
            </Box>
        </>
    );
};
export default MachinesList;
