<?php
require_once(LIB_PATH_INC . DS . 'config.php');

class MySqli_DB
{
  private static $instance = null; // Singleton instance
  private $con;
  public $query_id, $error;

  // Constructor private supaya tidak bisa di-instantiate sembarangan
  private function __construct()
  {
    $this->db_connect();
  }

  // Ambil instance tunggal
  public static function getInstance()
  {
    if (self::$instance === null) {
      self::$instance = new self();
    }
    return self::$instance;
  }

  /*--------------------------------------------------------------*/
  /* Function for Open database connection
  /*--------------------------------------------------------------*/
  private function db_connect()
  {
    // Persistent connection (pakai prefix "p:")
    $this->con = mysqli_connect('p:' . DB_HOST, DB_USER, DB_PASS, DB_NAME);

    if (!$this->con) {
      die("Database connection failed: " . mysqli_connect_error());
    }

    // Set charset biar aman untuk UTF-8
    $this->con->set_charset("utf8mb4");
  }

  /*--------------------------------------------------------------*/
  /* Function for Close database connection (optional)
  /*--------------------------------------------------------------*/
  public function db_disconnect()
  {
    if (isset($this->con)) {
      mysqli_close($this->con);
      unset($this->con);
      self::$instance = null; // reset instance
    }
  }

  /*--------------------------------------------------------------*/
  /* Function for mysqli query
  /*--------------------------------------------------------------*/
  public function query($sql)
  {
    if (trim($sql) != "") {
      $this->query_id = $this->con->query($sql);
    }

    if (!$this->query_id) {
      $this->error = $this->con->error; // Simpan pesan kesalahan
      die("MySQL Error: " . $this->error); // Cetak pesan kesalahan
    }

    return $this->query_id;
  }

  /*--------------------------------------------------------------*/
  /* Function for Query Helper
  /*--------------------------------------------------------------*/
  public function prepare($sql)
  {
    return $this->con->prepare($sql);
  }

  public function fetch_array($statement)
  {
    return mysqli_fetch_array($statement);
  }
  public function fetch_object($statement)
  {
    return mysqli_fetch_object($statement);
  }
  public function fetch_assoc($statement)
  {
    return mysqli_fetch_assoc($statement);
  }
  public function num_rows($statement)
  {
    return mysqli_num_rows($statement);
  }
  public function insert_id()
  {
    return mysqli_insert_id($this->con);
  }
  public function affected_rows()
  {
    return mysqli_affected_rows($this->con);
  }

  /*--------------------------------------------------------------*/
  /* Escape string
  /*--------------------------------------------------------------*/
  public function escape($str)
  {
    return $this->con->real_escape_string($str);
  }

  /*--------------------------------------------------------------*/
  /* While loop helper
  /*--------------------------------------------------------------*/
  public function while_loop($loop)
  {
    $results = array();
    while ($result = $this->fetch_array($loop)) {
      $results[] = $result;
    }
    return $results;
  }

  /*--------------------------------------------------------------*/
  /* Transaction functions
  /*--------------------------------------------------------------*/
  public function begin_transaction()
  {
    $this->con->begin_transaction();
  }

  public function commit()
  {
    $this->con->commit();
  }

  public function rollback()
  {
    $this->con->rollback();
  }
}

// Ambil koneksi tunggal
$db = MySqli_DB::getInstance();
