// resources\assets\ts\DataPage\actionSlice.ts
import { createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'B/axios';

import { RootState } from './rootReducer';
import { clearTable, markAsRead as markAsReadView } from './tableSlice';
import { fetchAll } from './dataSlice';
import { setClose } from './moveTaskDateSlice';
import { setTaskState } from '../share/aufgaben-shared/slices/filterSlice';

export const markAsRead = createAsyncThunk(
  'monitor/markAsRead',
  async (id: number, thunkAPI) => {
    await axios.post(`/c/${id}/read`).then(() => {
      thunkAPI.dispatch(markAsReadView(id));
    });
  }
);

export const reqestNextTaskWithoutForm = createAsyncThunk(
  'next_task/without_form',
  async (ergID: number, thunkAPI) => {
    await axios.post('/ProceedTask/doAction', {
      ergid: ergID,
      action: 'auto_next'
    });

    thunkAPI.dispatch(fetchAll());
  }
);

export const reqestQuitTask = createAsyncThunk(
  'quit_task',
  async (ergID: number, thunkAPI) => {
    await axios.post('/ProceedTask/doAction', {
      ergid: ergID,
      action: 'quit'
    });

    // ✅ Event already emitted from Detail.tsx handleQuitTask for Contactspage

    thunkAPI.dispatch(fetchAll());
  }
);

export const requestDeleteTask = createAsyncThunk(
  'delete_task',
  async (ergID: number, thunkAPI) => {
    await axios.post('/ProceedTask/doAction', {
      ergid: ergID,
      action: 'delete'
    });

        // ✅ ADD: Emit event for ContactsPage check first
    // window.dispatchEvent(new CustomEvent('contactDeleteTask', { 
    //   detail: { ergId: ergID } 
    // }));

    thunkAPI.dispatch(fetchAll());
  }
);

export const requestRecoverTask = createAsyncThunk(
  'recover_task',
  async (ergID: number, thunkAPI) => {
    await axios.post('/ProceedTask/doAction', {
      ergid: ergID,
      action: 'recover'
    });
    
    // ✅ Event already emitted from Detail.tsx handleRecoverTask for contaact page

    thunkAPI.dispatch(fetchAll());
  }
);

export const requestMoveTask = createAsyncThunk(
  'move_task',
  async (_arg, thunkAPI) => {
    const { moveTaskDateDialog } = thunkAPI.getState() as RootState;
    const { ergID, newDate } = moveTaskDateDialog;

    await axios.post('/ProceedTask/doAction', {
      datum: newDate,
      ergid: ergID,
      action: 'move'
    });

        // ✅ ADD: Emit event for ContactsPage //CMS-prototype
    window.dispatchEvent(new CustomEvent('contactMoveTask', { 
      detail: { ergId: ergID, newDate } 
    }));

    const d = thunkAPI.dispatch;
    d(setClose());
    d(fetchAll());
  }
);

export const requestMoveAGB = createAsyncThunk(
  'move_task_agb',
  async (_arg, thunkAPI) => {
    const { moveTaskDateDialog } = thunkAPI.getState() as RootState;
    const { agbID, newDate } = moveTaskDateDialog;

    await axios.post('/config/setDateAGB', { date: newDate, agb_id: agbID });

    const d = thunkAPI.dispatch;
    d(setClose());
    d(clearTable());
    d(setTaskState('upcoming'));
    d(fetchAll());
  }
);
