<?php
/**
 * Get Category Data for AJAX Edit
 * File: admin/get-category.php
 */

require_once '../app/Core/init.php';

// Check admin authentication
Auth::requireAdmin();

header('Content-Type: application/json');

try {
    $id = (int)($_GET['id'] ?? 0);
    
    if (!$id) {
        echo json_encode(['success' => false, 'message' => 'Invalid category ID']);
        exit;
    }
    
    $db = Database::getInstance();
    $sql = "SELECT * FROM categories WHERE id = ? LIMIT 1";
    $category = $db->fetch($sql, [$id]);
    
    if (!$category) {
        echo json_encode(['success' => false, 'message' => 'Category not found']);
        exit;
    }
    
    echo json_encode([
        'success' => true,
        'category' => $category
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error: ' . $e->getMessage()
    ]);
}
?>