<?php
require_once 'Logger.php';

class Category {
    private $db;
    private $logger;

    public function __construct($db, $logger = null) {
        $this->db = $db;
        $this->logger = $logger;
    }

    public function register($categoryname, $categorydescription) {
        $sql = "INSERT INTO category (category_name, category_description) VALUES (:categoryname, :categorydescription)";
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':categoryname', $categoryname);
        $stmt->bindParam(':categorydescription', $categorydescription);
        
        $result = $stmt->execute();

		
		/*
        if ($result && $this->logger) {
            $userId = $this->db->lastInsertId();
            $this->logger->logCategory('CREATE', $userId, [
                'username' => $username,
                'role' => $role,
                'email' => $email,
                'branch_id' => $branch_id
            ]);
        } */

        return $result;
    }

    public function login($username, $password) {
        $sql = "SELECT * FROM users WHERE username = :username";
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':username', $username);
        $stmt->execute();
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($user) {
            if (password_verify($password, $user['password'])) {
                return $user;
            } else {
                error_log("Password verification failed for username: $username");
            }
        } else {
            error_log("Category not found: $username");
        }
        return false;
    }

    public function getCategoryById($id) {
        $sql = "SELECT * FROM category WHERE category_id = :id";
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':id', $id);
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function updateCategory($id, $categoryname, $categorydescription) {
        // Get old values for logging
        $oldCategory = null;
        if ($this->logger) {
            $oldCategory = $this->getCategoryById($id);
        }

        $query = "UPDATE category SET category_name = :categoryname, category_description = :categorydescription";

       

        $query .= " WHERE category_id = :id";

        $stmt = $this->db->prepare($query);
        $stmt->bindParam(':categoryname', $categoryname);
        $stmt->bindParam(':categorydescription', $categorydescription);
        $stmt->bindParam(':id', $id);

       

        $result = $stmt->execute();

		/*
        if ($result && $this->logger && $oldCategory) {
            $newValues = [
                'username' => $username,
                'role' => $role,
                'email' => $email,
                'branch_id' => $branch_id
            ];
            if (!empty($password)) {
                $newValues['password'] = '***CHANGED***';
            }

            $this->logger->logCategory('UPDATE', $id, $newValues, $oldCategory);
        }
		
		*/

        return $result;
    }

    public function deleteCategory($id) {
        // Get user info for logging before deletion
        $userInfo = null;
        if ($this->logger) {
            $userInfo = $this->getCategoryById($id);
        }

        $query = "DELETE FROM users WHERE user_id = :id";
        $stmt = $this->db->prepare($query);
        $stmt->bindParam(':id', $id);
        $result = $stmt->execute();

        if ($result && $this->logger && $userInfo) {
            $this->logger->logCategory('DELETE', $id, null, $userInfo);
        }

        return $result;
    }

    public function getAllCategories() {
        $query = "SELECT * FROM category ";
        $stmt = $this->db->prepare($query);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function getCategoryBranch($userId) {
        $query = "SELECT branch_id FROM users WHERE user_id = :user_id";
        $stmt = $this->db->prepare($query);
        $stmt->bindParam(':user_id', $userId);
        $stmt->execute();
        return $stmt->fetchColumn();
    }

    public function isCategoryInBranch($userId, $branchId) {
        $query = "SELECT COUNT(*) FROM users WHERE user_id = :user_id AND branch_id = :branch_id";
        $stmt = $this->db->prepare($query);
        $stmt->bindParam(':user_id', $userId);
        $stmt->bindParam(':branch_id', $branchId);
        $stmt->execute();
        return $stmt->fetchColumn() > 0;
    }
}
?>
