import { DataGridColumnHeader, DataGridProvider } from '@/components';
import { Toaster } from '@/components/ui/sonner';
import { apiConfig } from '@/config/api.config';
import { useCallApi } from '@/hooks';
import { ColumnDef } from '@tanstack/react-table';
import { createContext, useCallback, useMemo, useState } from 'react';
import ListToolbar from '../blocks/ListToolBar';

interface SelectedUser {
  id: string;
  name: string;
  internal_name: string;
  description: string;
}

interface AccessTypeProps {
  id: string;
  name: string;
  internal_name: string;
  description: string;
}

interface ContextProps {
  showEditDialog: boolean;
  handleEditDialog: (show: boolean, selected_user: string | null) => void;
  showAddDialog: boolean;
  handleAddDialog: (show: boolean) => void;
  selectedUser: string | null;
  accessTypes: AccessTypeProps[];
}

const initialProps: ContextProps = {
  showEditDialog: false,
  handleEditDialog: () => {},
  showAddDialog: false,
  handleAddDialog: () => {},
  selectedUser: null,
  accessTypes: []
};

const ManageAccessTypeContext = createContext<ContextProps>(initialProps);
const API_URL = apiConfig.service_dashboard;

const ManageAccessTypeContextProvider = ({ children }: { children: React.ReactNode }) => {
  const [showEditDialog, setShowEditDialog] = useState(false);
  const [showAddDialog, setShowAddDialog] = useState(false);
  const [selectedUser, setSelectedUser] = useState<string | null>(null);
  const [accessTypes, setAccessTypes] = useState<AccessTypeProps[]>([]);
  const { GetData } = useCallApi();

  const handleEditDialog = useCallback((show: boolean, selected_user: string | null) => {
    setSelectedUser(show ? selected_user : null);
    setShowEditDialog(show);
  }, []);

  const handleAddDialog = useCallback((show: boolean) => {
    setShowAddDialog(show);
  }, []);

  const columns = useMemo<ColumnDef<any>[]>(
    () => [
      {
        accessorFn: (row) => row.id,
        id: 'id',
        header: ({ column }) => <DataGridColumnHeader title="ID" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[100px]'
        }
      },
      {
        accessorFn: (row) => row.name,
        id: 'name',
        header: ({ column }) => <DataGridColumnHeader title="Name" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        accessorFn: (row) => row.internal_name,
        id: 'internal_name',
        header: ({ column }) => <DataGridColumnHeader title="Internal Name" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        accessorFn: (row) => row.description,
        id: 'description',
        header: ({ column }) => <DataGridColumnHeader title="Description" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        id: 'actions',
        enableSorting: false,
        header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
        cell: ({ row }) => {
          return (
            <>
              <button
                className="btn btn-sm btn-primary mr-2"
                onClick={() => handleEditDialog(true, row.original.id)}
              >
                Edit
              </button>
              <button className="btn btn-sm btn-primary" onClick={() => handleAddDialog(true)}>
                Add
              </button>
            </>
          );
        },
        meta: {
          headerClassName: 'w-[100px]',
          cellClassName: 'text-center'
        }
      }
    ],
    [handleAddDialog, handleEditDialog]
  );

  return (
    <div>
      <div className="container mx-auto py-5">
        <h1>Manage Access Type</h1>
      </div>
      <ManageAccessTypeContext.Provider
        value={{
          showEditDialog,
          handleEditDialog,
          showAddDialog,
          handleAddDialog,
          selectedUser,
          accessTypes
        }}
      >
        <Toaster expand visibleToasts={9} duration={3000} />
        <DataGridProvider
          columns={columns}
          pagination={{ size: 10 }}
          layout={{ card: true }}
          toolbar={<ListToolbar />}
          sorting={[{ id: 'id', desc: true }]}
          serverSide={true}
        >
          {children}
        </DataGridProvider>
      </ManageAccessTypeContext.Provider>
    </div>
  );
};

export { ManageAccessTypeContext, ManageAccessTypeContextProvider };
export type { SelectedUser };
