import API from '../api/API';
import { toast } from 'sonner';

export const approvalPolicyService = {
  async getPolicies() {
    try {
      const response: any = await API.getApprovalPolicies();
      return response.data;
    } catch (error: any) {
      toast.error(error?.response?.data?.message || 'Failed to fetch approval policies');
      return { success: false, data: [] };
    }
  },

  async savePolicy(stageCode: string, payload: any) {
    try {
      const response: any = await API.upsertApprovalPolicy(stageCode, payload);
      if (response?.data?.success) {
        toast.success('Approval policy updated');
      }
      return response.data;
    } catch (error: any) {
      toast.error(error?.response?.data?.message || 'Failed to update approval policy');
      return { success: false };
    }
  }
};
