<?php
require_once 'config.php';

class Database {
    private $conn;

    public function __construct() {
        $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        
        if ($this->conn->connect_error) {
            die("Error de conexión: " . $this->conn->connect_error);
        }
        
        $this->conn->set_charset("utf8");
        $this->conn->autocommit(true);
    }

    public function lastInsertId() {
        return $this->conn->insert_id;
    }

    public function query($sql, $params = []) {
        $stmt = $this->conn->prepare($sql);
        
        if (!$stmt) {
            die("Error en la consulta: " . $this->conn->error);
        }
        
        if (!empty($params)) {
            $types = str_repeat('s', count($params));
            $stmt->bind_param($types, ...$params);
        }
        
        $stmt->execute();
        return $stmt;
    }

    public function fetchAll($sql, $params = []) {
        $stmt = $this->query($sql, $params);
        $result = $stmt->get_result();
        return $result->fetch_all(MYSQLI_ASSOC);
    }

    public function fetchOne($sql, $params = []) {
        $stmt = $this->query($sql, $params);
        $result = $stmt->get_result();
        return $result->fetch_assoc();
    }

    public function insert($table, $data) {
        $keys = array_keys($data);
        $values = array_values($data);
        $placeholders = implode(',', array_fill(0, count($values), '?'));
        
        $sql = "INSERT INTO $table (" . implode(',', $keys) . ") VALUES ($placeholders)";
        $stmt = $this->query($sql, $values);
        
        return $this->conn->insert_id;
    }

    public function update($table, $data, $condition, $params = []) {
        $set = [];
        foreach ($data as $key => $value) {
            $set[] = "$key = ?";
        }
        $values = array_values($data);
        
        // Manejar condición como array asociativo
        $whereClause = '';
        $whereValues = [];
        
        if (is_array($condition)) {
            $whereParts = [];
            foreach ($condition as $key => $value) {
                $whereParts[] = "$key = ?";
                $whereValues[] = $value;
            }
            $whereClause = implode(' AND ', $whereParts);
        } else {
            $whereClause = $condition;
            $whereValues = $params;
        }
        
        $sql = "UPDATE $table SET " . implode(',', $set) . " WHERE $whereClause";
        $allParams = array_merge($values, $whereValues);
        
        $stmt = $this->query($sql, $allParams);
        return $stmt->affected_rows;
    }

    public function delete($table, $condition, $params = []) {
        $sql = "DELETE FROM $table WHERE $condition";
        $stmt = $this->query($sql, $params);
        return $stmt->affected_rows;
    }

    public function beginTransaction() {
        $this->conn->autocommit(false);
        return true;
    }
    
    public function commit() {
        $result = $this->conn->commit();
        $this->conn->autocommit(true);
        return $result;
    }
    
    public function rollBack() {
        $result = $this->conn->rollback();
        $this->conn->autocommit(true);
        return $result;
    }
    
    public function close() {
        $this->conn->close();
    }
}

$db = new Database();
?>