<?php
class Category {
    private $conn;
    private $table_name = "categories";

    public $id;
    public $name;
    public $description;
    public $image;
    public $type;
    public $is_active;

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

    // Create category
    public function create() {
        $query = "INSERT INTO " . $this->table_name . "
                SET name=:name, description=:description, image=:image, type=:type, is_active=:is_active";

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

        // Sanitize
        $this->name = htmlspecialchars(strip_tags($this->name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->image = htmlspecialchars(strip_tags($this->image ?? ''));
        $this->type = htmlspecialchars(strip_tags($this->type));
        $this->is_active = (bool)$this->is_active;

        // Bind values
        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":image", $this->image);
        $stmt->bindParam(":type", $this->type);
        $stmt->bindParam(":is_active", $this->is_active);

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

    // Read all categories
    public function readAll($type = null) {
        $query = "SELECT * FROM " . $this->table_name;

        if ($type) {
            $query .= " WHERE type = :type";
        }

        $query .= " ORDER BY created_at DESC";

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

        if ($type) {
            $stmt->bindParam(":type", $type);
        }

        $stmt->execute();
        return $stmt;
    }

    // Read one category
    public function readOne() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE 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->image = $row['image'];
            $this->type = $row['type'];
            $this->is_active = $row['is_active'];
            return true;
        }

        return false;
    }

    // Update category
    public function update() {
        $query = "UPDATE " . $this->table_name . "
                SET name=:name, description=:description, image=:image, type=:type, is_active=:is_active
                WHERE id=:id";

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

        // Sanitize
        $this->name = htmlspecialchars(strip_tags($this->name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->image = htmlspecialchars(strip_tags($this->image ?? ''));
        $this->type = htmlspecialchars(strip_tags($this->type));
        $this->is_active = (bool)$this->is_active;

        // Bind values
        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":image", $this->image);
        $stmt->bindParam(":type", $this->type);
        $stmt->bindParam(":is_active", $this->is_active);
        $stmt->bindParam(":id", $this->id);

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

    // Delete category
    public function delete() {
        // Check if category has products or services
        if ($this->hasItems()) {
            return false; // Cannot delete category with items
        }

        $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;
    }

    // Check if category has items (products or services)
    public function hasItems() {
        $query = "";
        if ($this->type === 'product') {
            $query = "SELECT COUNT(*) as count FROM products WHERE category_id = ?";
        } elseif ($this->type === 'service') {
            $query = "SELECT COUNT(*) as count FROM beauty_services WHERE category = ?";
        }

        if (!empty($query)) {
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(1, $this->id);
            $stmt->execute();
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            return $row['count'] > 0;
        }

        return false;
    }

    // Get category name for service comparison
    public function getCategoryName() {
        $query = "SELECT name FROM " . $this->table_name . " WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row ? $row['name'] : '';
    }

    // Get category statistics
    public function getStats() {
        $stats = [];

        if ($this->type === 'product') {
            // Count products in this category
            $query = "SELECT COUNT(*) as count FROM products WHERE category_id = ?";
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(1, $this->id);
            $stmt->execute();
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            $stats['products_count'] = $row['count'];
        } elseif ($this->type === 'service') {
            // Count services in this category
            $category_name = $this->getCategoryName();
            $query = "SELECT COUNT(*) as count FROM beauty_services WHERE category = ?";
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(1, $category_name);
            $stmt->execute();
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            $stats['services_count'] = $row['count'];
        }

        return $stats;
    }

    // Search categories
    public function search($keywords) {
        $query = "SELECT * FROM " . $this->table_name . "
                WHERE name LIKE ? OR description LIKE ?
                ORDER BY created_at DESC";

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

        $keywords = htmlspecialchars(strip_tags($keywords));
        $keywords = "%{$keywords}%";

        $stmt->bindParam(1, $keywords);
        $stmt->bindParam(2, $keywords);

        $stmt->execute();
        return $stmt;
    }

    // Toggle active status
    public function toggleActive() {
        $query = "UPDATE " . $this->table_name . " SET is_active = NOT is_active WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);

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