// redux/thunks/getLast20MessagesThunk.ts
import { createAsyncThunk } from "@reduxjs/toolkit";
import { fetchMessagesService } from "@/services/fetchMessagesService";

export const getLast20MessagesThunk = createAsyncThunk(
  "last20Messages/fetch",
  async () => {
    const toDate = new Date();
    const fromDate = new Date();
    fromDate.setDate(toDate.getDate() - 30); // z.B. letzte 30 Tage

    const allMessages = await fetchMessagesService(
      fromDate.toISOString().split("T")[0],
      toDate.toISOString().split("T")[0]
    );

    const last20 = allMessages.slice(-20);
    console.log("Last 20 messages in thunk:", last20);
    return last20;
  }
);
