<?php
class Ingredient { private $pdo; public function __construct(){ $this->pdo=$GLOBALS['pdo']; }
  public function all(){ return $this->pdo->query("SELECT * FROM ingredients ORDER BY name ASC")->fetchAll(); }
  public function find($id){ $st=$this->pdo->prepare("SELECT * FROM ingredients WHERE id=?"); $st->execute([$id]); return $st->fetch(); }
  public function create($d){ 
    $st=$this->pdo->prepare("INSERT INTO ingredients (name,unit,supplier,notes,low_stock,high_stock,critical_low_stock,critical_high_stock) VALUES (?,?,?,?,?,?,?,?)"); 
    return $st->execute([
      $d['name'],
      $d['unit']??'', 
      $d['supplier']??'', 
      $d['notes']??'',
      $d['low_stock']??null,
      $d['high_stock']??null,
      $d['critical_low_stock']??null,
      $d['critical_high_stock']??null
    ]); 
  }
  public function update($id,$d){ 
    $st=$this->pdo->prepare("UPDATE ingredients SET name=?, unit=?, supplier=?, notes=?, low_stock=?, high_stock=?, critical_low_stock=?, critical_high_stock=? WHERE id=?"); 
    return $st->execute([
      $d['name'],
      $d['unit']??'', 
      $d['supplier']??'', 
      $d['notes']??'',
      $d['low_stock']??null,
      $d['high_stock']??null,
      $d['critical_low_stock']??null,
      $d['critical_high_stock']??null,
      $id
    ]); 
  }
  public function delete($id){ $st=$this->pdo->prepare("DELETE FROM ingredients WHERE id=?"); return $st->execute([$id]); }
}

