<?php

namespace EasyRestaurantManager\Models;

class Category extends Model
{
    protected $model = 'erm_menu_categories';

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

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

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

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

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

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