<?php
class Product {
    private $conn;
    private $table_name = "products";

    public $id;
    public $name;
    public $description;
    public $price;
    public $image;
    public $category_id;
    public $stock_quantity;
    public $is_limited_stock;
    public $is_free_shipping;
    public $is_medicinal;
    public $medicinal_leaflet;
    public $usage_instructions;
    public $variations;
    public $additional_images;

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

    public function getAll() {
        $query = "SELECT p.id, p.name, p.description, p.price, p.image, p.stock_quantity, p.additional_images, c.name as category_name
                FROM " . $this->table_name . " p
                LEFT JOIN categories c ON p.category_id = c.id
                ORDER BY p.created_at DESC";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();

        return $stmt;
    }

    public function getOne() {
        $query = "SELECT p.id, p.name, p.description, p.price, p.image, p.category_id, p.stock_quantity, c.name as category_name
                FROM " . $this->table_name . " p
                LEFT JOIN categories c ON p.category_id = c.id
                WHERE p.id = ?
                LIMIT 0,1";

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

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if($row) {
            $this->name = $row['name'];
            $this->description = $row['description'];
            $this->price = $row['price'];
            $this->image = $row['image'];
            $this->category_id = $row['category_id'];
            $this->stock_quantity = $row['stock_quantity'];

            // Try to get new columns, fallback to defaults if they don't exist
            $this->is_limited_stock = $row['is_limited_stock'] ?? false;
            $this->is_free_shipping = $row['is_free_shipping'] ?? false;
            $this->is_medicinal = $row['is_medicinal'] ?? false;
            $this->medicinal_leaflet = $row['medicinal_leaflet'] ?? '';
            $this->usage_instructions = $row['usage_instructions'] ?? '';
            $this->variations = isset($row['variations']) ? json_decode($row['variations'], true) : [];
            $this->additional_images = isset($row['additional_images']) ? json_decode($row['additional_images'], true) : [];
        }

        return $row;
    }

    public function create() {
        $query = "INSERT INTO " . $this->table_name . "
                SET name=:name, description=:description, price=:price, image=:image, category_id=:category_id, stock_quantity=:stock_quantity, is_limited_stock=:is_limited_stock, is_free_shipping=:is_free_shipping, is_medicinal=:is_medicinal, medicinal_leaflet=:medicinal_leaflet, usage_instructions=:usage_instructions, variations=:variations, additional_images=:additional_images";

        $stmt = $this->conn->prepare($query);

        $this->name = htmlspecialchars(strip_tags($this->name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->price = htmlspecialchars(strip_tags($this->price));
        $this->image = htmlspecialchars(strip_tags($this->image));
        $this->category_id = htmlspecialchars(strip_tags($this->category_id));
        $this->stock_quantity = htmlspecialchars(strip_tags($this->stock_quantity));
        $this->medicinal_leaflet = htmlspecialchars(strip_tags($this->medicinal_leaflet ?? ''));
        $this->usage_instructions = htmlspecialchars(strip_tags($this->usage_instructions ?? ''));

        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":price", $this->price);
        $stmt->bindParam(":image", $this->image);
        $stmt->bindParam(":category_id", $this->category_id);
        $stmt->bindParam(":stock_quantity", $this->stock_quantity);
        $stmt->bindParam(":is_limited_stock", $this->is_limited_stock);
        $stmt->bindParam(":is_free_shipping", $this->is_free_shipping);
        $stmt->bindParam(":is_medicinal", $this->is_medicinal);
        $stmt->bindParam(":medicinal_leaflet", $this->medicinal_leaflet);
        $stmt->bindParam(":usage_instructions", $this->usage_instructions);
        $variations_json = json_encode($this->variations ?: []);
        $additional_images_json = json_encode($this->additional_images ?: []);

        $stmt->bindParam(":variations", $variations_json);
        $stmt->bindParam(":additional_images", $additional_images_json);

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

        return false;
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . "
                SET name=:name, description=:description, price=:price, image=:image, category_id=:category_id, stock_quantity=:stock_quantity, is_limited_stock=:is_limited_stock, is_free_shipping=:is_free_shipping, is_medicinal=:is_medicinal, medicinal_leaflet=:medicinal_leaflet, usage_instructions=:usage_instructions, variations=:variations, additional_images=:additional_images
                WHERE id=:id";

        $stmt = $this->conn->prepare($query);

        $this->name = htmlspecialchars(strip_tags($this->name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->price = htmlspecialchars(strip_tags($this->price));
        $this->image = htmlspecialchars(strip_tags($this->image));
        $this->category_id = htmlspecialchars(strip_tags($this->category_id));
        $this->stock_quantity = htmlspecialchars(strip_tags($this->stock_quantity));
        $this->medicinal_leaflet = htmlspecialchars(strip_tags($this->medicinal_leaflet ?? ''));
        $this->usage_instructions = htmlspecialchars(strip_tags($this->usage_instructions ?? ''));

        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":price", $this->price);
        $stmt->bindParam(":image", $this->image);
        $stmt->bindParam(":category_id", $this->category_id);
        $stmt->bindParam(":stock_quantity", $this->stock_quantity);
        $stmt->bindParam(":is_limited_stock", $this->is_limited_stock);
        $stmt->bindParam(":is_free_shipping", $this->is_free_shipping);
        $stmt->bindParam(":is_medicinal", $this->is_medicinal);
        $stmt->bindParam(":medicinal_leaflet", $this->medicinal_leaflet);
        $stmt->bindParam(":usage_instructions", $this->usage_instructions);
        $variations_json = json_encode($this->variations ?: []);
        $additional_images_json = json_encode($this->additional_images ?: []);

        $stmt->bindParam(":variations", $variations_json);
        $stmt->bindParam(":additional_images", $additional_images_json);
        $stmt->bindParam(":id", $this->id);

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

        return false;
    }

    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";

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

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

        return false;
    }

    public function getOneMinimal() {
        $query = "SELECT id, stock_quantity FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";

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

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