<?php

namespace EasyRestaurantManager\Models;

class Coupon extends Model
{
    protected $model = 'erm_coupons';

    public static function store($coupon)
    {
        global $wpdb;
        $wpdb->insert($wpdb->prefix . 'erm_coupons', $coupon);
        return $wpdb->insert_id;
    }

    public static function updateCoupon($coupon)
    {
        global $wpdb;
        if (!isset($coupon['id'])) {
            return ('missing id');
        }
        $id = (int) $coupon['id']; 
        unset($coupon['id']); 

        $updated = $wpdb->update($wpdb->prefix . 'erm_coupons',$coupon, ['id' => $id],null, ['%d']
        );

        if ($updated === false) {
            return ('update failed');
        }
        return $updated;
    }

    public static function getCoupon(){
        global $wpdb;
        $res = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}erm_coupons  ORDER BY id DESC");
        return $res;
    }

    public static function deleteCoupon($id){
        global $wpdb;
        $res = $wpdb->delete($wpdb->prefix . 'erm_coupons', ['id' => $id]);
        return $res;
    }
  
}
