import { createAsyncThunk } from '@reduxjs/toolkit';
import { API } from './index';

import {
  {{pascalCase model}},
  {{pascalCase model}}Relationships,
} from '../models';

import {
  DeleteResponse,
  ErrorResponse,
  GetAllResponse,
  GetOneResponse,
} from '../requests';

import { RootState } from '../store';

export const getAll{{pascalCase model}}s =
  createAsyncThunk<
    GetAllResponse<{{pascalCase model}}>,
    undefined,
    { rejectValue: ErrorResponse }
  >(
    '{{camelCase model}}/getAll{{pascalCase model}}s',
    async (_, { getState, rejectWithValue }) => {
      const {} = getState() as RootState;

      try {
        const { data } = await API(
          'get',
          '{{dashCase model}}s'
        );

        return data;
      } catch (err: any) {
        return rejectWithValue(err.response.data as ErrorResponse);
      }
    }
  );

export const getOne{{pascalCase model}} =
  createAsyncThunk<
    GetOneResponse<{{pascalCase model}}>,
    number,
    { rejectValue: ErrorResponse }
  >(
    '{{camelCase model}}/getOne{{pascalCase model}}',
    async (id, { getState, rejectWithValue }) => {
      const {} = getState() as RootState;

      try {
        const { data } = await API(
          'get',
          `{{dashCase model}}s/${id}`
        );

        return data;
      } catch (err: any) {
        return rejectWithValue(err.response.data as ErrorResponse);
      }
    }
  );

export const create{{pascalCase model}} =
  createAsyncThunk<
    GetOneResponse<{{pascalCase model}}>,
    undefined,
    { rejectValue: ErrorResponse }
  >(
    '{{camelCase model}}/create{{pascalCase model}}',
    async (_, { getState, rejectWithValue }) => {
      const {
        {{camelCase model}}: {
          create{{pascalCase model}},
        },
      } = getState() as RootState;

      try {
        const { data } = await API(
          'post',
          '{{dashCase model}}s',
          create{{pascalCase model}}
        );

        return data;
      } catch (err: any) {
        return rejectWithValue(err.response.data as ErrorResponse);
      }
    }
  );

export const update{{pascalCase model}} =
  createAsyncThunk<
    GetOneResponse<{{pascalCase model}}>,
    number,
    { rejectValue: ErrorResponse }
  >(
    '{{camelCase model}}/update{{pascalCase model}}',
    async (id, { getState, rejectWithValue }) => {
      const {
        {{camelCase model}}: {
          update{{pascalCase model}},
        },
      } = getState() as RootState;

      try {
        const { data } = await API(
          'put',
          `{{dashCase model}}s/${id}`,
          update{{pascalCase model}}
        );

        return data;
      } catch (err: any) {
        return rejectWithValue(err.response.data as ErrorResponse);
      }
    }
  );

export const delete{{pascalCase model}} =
  createAsyncThunk<DeleteResponse, number, { rejectValue: ErrorResponse }>(
    '{{camelCase model}}/delete{{pascalCase model}}',
    async (id, { getState, rejectWithValue }) => {
      const {} = getState() as RootState;

      try {
        const { data } = await API(
          'delete',
          `{{dashCase model}}s/${id}`
        );

        return data;
      } catch (err: any) {
        return rejectWithValue(err.response.data as ErrorResponse);
      }
    }
  );
