<?php
// pages/components/coupon_batch_delete.php
use Components\Models\CouponBatch;
use Components\Support\Htmx;

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    exit;
}

$id = (int)mh_get('id', 0, 'int');
if ($id > 0) {
    $batch = CouponBatch::find($id);
    if ($batch) {
        // Clear batch_id from tracking coupons
        if (defined('TABLE_MAILBEEZ_TRACKING_COUPONS')) {
            mh_db_query("UPDATE " . TABLE_MAILBEEZ_TRACKING_COUPONS . " SET batch_id = NULL WHERE batch_id = " . $id);
        }
        
        $batch->delete();
        Htmx::trigger('notification', ['level' => 'success', 'message' => 'Coupon series deleted.']);
    }
}

// Re-render the list
require BEEZUI_ROOT . '/pages/components/coupon_batches_list.php';
