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

export type ChickinApprovalSlice = {
  // State
  isChickinApproveModalOpen: boolean;
  selectedChickinForApproval: ProjectFlockKandang | null;
  isChickinApproveLoading: boolean;
  chickinApproveCallback: ((notes?: string) => Promise<void>) | null;

  // Actions
  openChickinApproveModal: (
    data: ProjectFlockKandang,
    callback: (notes?: string) => Promise<void>
  ) => void;
  closeChickinApproveModal: () => void;
  setChickinApproveLoading: (loading: boolean) => void;
  resetChickinApproval: () => void;
};

export const createChickinApprovalSlice: StateCreator<
  ChickinApprovalSlice,
  [],
  [],
  ChickinApprovalSlice
> = (set) => ({
  // Initial state
  isChickinApproveModalOpen: false,
  selectedChickinForApproval: null,
  isChickinApproveLoading: false,
  chickinApproveCallback: null,

  // Actions
  openChickinApproveModal: (data, callback) =>
    set({
      isChickinApproveModalOpen: true,
      selectedChickinForApproval: data,
      chickinApproveCallback: callback,
    }),

  closeChickinApproveModal: () =>
    set({
      isChickinApproveModalOpen: false,
      selectedChickinForApproval: null,
      chickinApproveCallback: null,
    }),

  setChickinApproveLoading: (loading) =>
    set({ isChickinApproveLoading: loading }),

  resetChickinApproval: () =>
    set({
      isChickinApproveModalOpen: false,
      selectedChickinForApproval: null,
      isChickinApproveLoading: false,
      chickinApproveCallback: null,
    }),
});
