'use server';

import { applyPromotions } from '@lib/data/cart';

export async function addPromotionCodeAction(code: string) {
  try {
    await applyPromotions([code]);
    return { success: true, message: 'Discount code applied successfully' };
  } catch (error) {
    console.error('Error applying promotion code:', error);
    return { success: false, message: 'Failed to apply discount code' };
  }
}

export async function removePromotionCodeAction(
  codeToRemove: string,
  appliedCodes: string[],
) {
  try {
    const updatedCodes = appliedCodes.filter(
      (appliedCode) => appliedCode !== codeToRemove,
    );
    await applyPromotions(updatedCodes);
    return { success: true, message: 'Discount code removed successfully' };
  } catch (error) {
    console.error('Error removing promotion code:', error);
    return { success: false, message: 'Failed to remove discount code' };
  }
}
