import { createAsyncThunk } from "@reduxjs/toolkit";
import type { RootState } from "app/store";
import { openSnackbar } from "features/ui/uiSlice";
import { getTranslationID } from "translation";
import {
  AccountsApi,
  GetTypistGroupsResponse,
  GetTypistsResponse,
  CreateTypistGroupRequest,
} from "../../../api/api";
import { Configuration } from "../../../api/configuration";
import { ErrorObject, createErrorObject } from "../../../common/errors";

export const listTypistGroupsAsync = createAsyncThunk<
  GetTypistGroupsResponse,
  void,
  {
    // rejectした時の返却値の型
    rejectValue: {
      error: ErrorObject;
    };
  }
>("workflow/listTypistGroupsAsync", async (args, thunkApi) => {
  // apiのConfigurationを取得する
  const { getState } = thunkApi;
  const state = getState() as RootState;
  const { configuration, accessToken } = state.auth;
  const config = new Configuration(configuration);
  const accountsApi = new AccountsApi(config);

  try {
    const typistGroup = await accountsApi.getTypistGroups({
      headers: { authorization: `Bearer ${accessToken}` },
    });

    return typistGroup.data;
  } catch (e) {
    // e ⇒ errorObjectに変換"
    const error = createErrorObject(e);
    thunkApi.dispatch(
      openSnackbar({
        level: "error",
        message: getTranslationID("common.message.internalServerError"),
      })
    );
    return thunkApi.rejectWithValue({ error });
  }
});

export const listTypistsAsync = createAsyncThunk<
  GetTypistsResponse,
  void,
  {
    // rejectした時の返却値の型
    rejectValue: {
      error: ErrorObject;
    };
  }
>("workflow/listTypistsAsync", async (args, thunkApi) => {
  // apiのConfigurationを取得する
  const { getState } = thunkApi;
  const state = getState() as RootState;
  const { configuration, accessToken } = state.auth;
  const config = new Configuration(configuration);
  const accountsApi = new AccountsApi(config);

  try {
    const typists = await accountsApi.getTypists({
      headers: { authorization: `Bearer ${accessToken}` },
    });

    return typists.data;
  } catch (e) {
    // e ⇒ errorObjectに変換"
    const error = createErrorObject(e);
    thunkApi.dispatch(
      openSnackbar({
        level: "error",
        message: getTranslationID("common.message.internalServerError"),
      })
    );
    return thunkApi.rejectWithValue({ error });
  }
});

export const createTypistGroupAsync = createAsyncThunk<
  {
    /* Empty Object */
  },
  CreateTypistGroupRequest,
  {
    // rejectした時の返却値の型
    rejectValue: {
      error: ErrorObject;
    };
  }
>("workflow/createTypistGroupAsync", async (args, thunkApi) => {
  // apiのConfigurationを取得する
  const { getState } = thunkApi;
  const state = getState() as RootState;
  const { configuration, accessToken } = state.auth;
  const config = new Configuration(configuration);
  const accountsApi = new AccountsApi(config);

  try {
    await accountsApi.createTypistGroup(args, {
      headers: { authorization: `Bearer ${accessToken}` },
    });
    thunkApi.dispatch(
      openSnackbar({
        level: "info",
        message: getTranslationID("common.message.success"),
      })
    );
    return {};
  } catch (e) {
    // e ⇒ errorObjectに変換"
    const error = createErrorObject(e);

    const message =
      error.statusCode === 400
        ? getTranslationID("typistGroupSetting.message.groupAddFailedError")
        : getTranslationID("common.message.internalServerError");

    thunkApi.dispatch(
      openSnackbar({
        level: "error",
        message,
      })
    );
    return thunkApi.rejectWithValue({ error });
  }
});
