<?php
class admin_model {
    private $db;
    public function __construct() {
        $this->db = database::connect();
    }

    public function check_email($email, $id=null) {

      if (!empty($id)) {

      $stmt = $this->db->prepare("SELECT * FROM admin WHERE email = ? AND id != ?");

      $stmt->execute([$email, $id]);

      return $stmt->fetch(PDO::FETCH_ASSOC);

      }else{

      $stmt = $this->db->prepare("SELECT * FROM admin WHERE email = ?");

      $stmt->execute([$email]);

      return $stmt->fetch(PDO::FETCH_ASSOC);

      }

    }

    public function get($id) {
        $stmt = $this->db->prepare("SELECT * FROM admin WHERE id = ?");
        $stmt->execute([$id]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function update($id, $data) {
        $fields = implode(',', array_map(fn($k) => "$k = ?", array_keys($data)));
        $stmt = $this->db->prepare("UPDATE admin SET $fields WHERE id = ?");
        return $stmt->execute([...array_values($data), $id]);
    }

}
?>
