/**
 * Roles Redux Slice
 */

import { createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';
import { createEntitySlice } from '../createEntitySlice';
import type { Role } from '../../types/entities';

// Create base entity slice with standard CRUD operations
const {
  slice,
  actions,
  reducer: baseReducer,
} = createEntitySlice<Role>({
  name: 'roles',
  endpoint: 'roles',
  singularName: 'Role',
});

// Custom thunks specific to roles
export const removeWidget = createAsyncThunk(
  'openai/removeWidget',
  async (payload: { id: string; roleId: string; widgetId: string }) => {
    const result = await axios.delete(`openai/roles-info/${payload.id}`, {
      params: {
        roleId: payload.roleId,
        infoId: payload.widgetId,
        key: 'widgets',
      },
    });
    return result.data;
  },
);

export const fetchWidgets = createAsyncThunk(
  'openai/fetchWidgets',
  async (roleId: string) => {
    const result = await axios.get(
      `openai/info-by-key?key=widgets&roleId=${roleId}`,
    );
    return result.data;
  },
);

// Export standard CRUD actions
export const {
  fetch,
  create,
  update,
  deleteItem,
  deleteItemsByIds,
  uploadCsv,
  setRefetch,
} = actions;
export const rolesSlice = slice;

export default baseReducer;
