<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
class Cart {
    private $conn;
    private $table_name = "cart";

    public $id;
    public $user_id;
    public $product_id;
    public $quantity;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function getCart($user_id) {
        $query = "SELECT c.id, c.quantity, p.id as product_id, p.name, p.price, p.image
                FROM " . $this->table_name . " c
                LEFT JOIN products p ON c.product_id = p.id
                WHERE c.user_id = ?
                ORDER BY c.created_at DESC";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);
        $stmt->execute();

        return $stmt;
    }

    public function addToCart() {
        // Check if item already exists in cart
        $query = "SELECT id, quantity FROM " . $this->table_name . " WHERE user_id = ? AND product_id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->user_id);
        $stmt->bindParam(2, $this->product_id);
        $stmt->execute();

        if ($stmt->rowCount() > 0) {
            // Update quantity
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            $new_quantity = $row['quantity'] + $this->quantity;

            $query = "UPDATE " . $this->table_name . " SET quantity = ? WHERE id = ?";
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(1, $new_quantity);
            $stmt->bindParam(2, $row['id']);
        } else {
            // Insert new item
            $query = "INSERT INTO " . $this->table_name . " SET user_id=:user_id, product_id=:product_id, quantity=:quantity";
            $stmt = $this->conn->prepare($query);

            $stmt->bindParam(":user_id", $this->user_id);
            $stmt->bindParam(":product_id", $this->product_id);
            $stmt->bindParam(":quantity", $this->quantity);
        }

        if($stmt->execute()) {
            // Log the action
            $log_message = "Cart action: user_id={$this->user_id}, product_id={$this->product_id}, quantity={$this->quantity}" . PHP_EOL;
            file_put_contents('cart_log.txt', $log_message, FILE_APPEND);
            return true;
        }

        return false;
    }

    public function updateQuantity() {
        $query = "UPDATE " . $this->table_name . " SET quantity = ? WHERE id = ? AND user_id = ?";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(1, $this->quantity);
        $stmt->bindParam(2, $this->id);
        $stmt->bindParam(3, $this->user_id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function removeFromCart() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ? AND user_id = ?";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(1, $this->id);
        $stmt->bindParam(2, $this->user_id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function clearCart($user_id) {
        $query = "DELETE FROM " . $this->table_name . " WHERE user_id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function getCartByUserId($user_id) {
        $query = "SELECT c.id, c.quantity, p.id as product_id, p.name, p.price, p.image, p.description
                  FROM " . $this->table_name . " c
                  LEFT JOIN products p ON c.product_id = p.id
                  WHERE c.user_id = ?
                  ORDER BY c.created_at DESC";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);
        $stmt->execute();

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function getCartCount($user_id) {
        $query = "SELECT SUM(quantity) as total_items FROM " . $this->table_name . " WHERE user_id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $user_id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row['total_items'] ?? 0;
    }
}
?>