import { StateCreator } from 'zustand';
import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang';

export type ProjectFlockClosingSlice = {
  // State
  isClosingModalOpen: boolean;
  selectedProjectFlockKandang: ProjectFlockKandang | null;
  projectFlockId: number | null;
  isKandangClosed: boolean;
  isClosingLoading: boolean;
  closingCallback: ((action: 'close' | 'unclose') => Promise<void>) | null;

  // Actions
  openClosingModal: (
    data: ProjectFlockKandang,
    projectFlockId: number,
    isClosed: boolean,
    callback: (action: 'close' | 'unclose') => Promise<void>
  ) => void;
  closeClosingModal: () => void;
  setClosingLoading: (loading: boolean) => void;
  resetClosing: () => void;
};

export const createProjectFlockClosingSlice: StateCreator<
  ProjectFlockClosingSlice,
  [],
  [],
  ProjectFlockClosingSlice
> = (set) => ({
  // Initial state
  isClosingModalOpen: false,
  selectedProjectFlockKandang: null,
  projectFlockId: null,
  isKandangClosed: false,
  isClosingLoading: false,
  closingCallback: null,

  // Actions
  openClosingModal: (data, projectFlockId, isClosed, callback) =>
    set({
      isClosingModalOpen: true,
      selectedProjectFlockKandang: data,
      projectFlockId,
      isKandangClosed: isClosed,
      closingCallback: callback,
    }),

  closeClosingModal: () =>
    set({
      isClosingModalOpen: false,
      selectedProjectFlockKandang: null,
      projectFlockId: null,
      isKandangClosed: false,
      closingCallback: null,
    }),

  setClosingLoading: (loading) => set({ isClosingLoading: loading }),

  resetClosing: () =>
    set({
      isClosingModalOpen: false,
      selectedProjectFlockKandang: null,
      projectFlockId: null,
      isKandangClosed: false,
      isClosingLoading: false,
      closingCallback: null,
    }),
});
