<?php
require_once '../config.php';

$method = $_SERVER['REQUEST_METHOD'];
$pdo = getDBConnection();

switch($method) {
    case 'GET':
        if(isset($_GET['id'])) {
            // Get single equipment type
            $stmt = $pdo->prepare("SELECT * FROM equipment_types WHERE type_id = ?");
            $stmt->execute([$_GET['id']]);
            $type = $stmt->fetch();
            echo json_encode($type);
        } else {
            // Get all equipment types
            $stmt = $pdo->query("SELECT * FROM equipment_types ORDER BY type_name");
            $types = $stmt->fetchAll();
            echo json_encode($types);
        }
        break;
        
    case 'POST':
        // Create new equipment type
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("INSERT INTO equipment_types (type_name) VALUES (?)");
        $stmt->execute([$data['type_name']]);
        echo json_encode(['id' => $pdo->lastInsertId(), 'message' => 'Equipment type created successfully']);
        break;
        
    case 'PUT':
        // Update equipment type
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("UPDATE equipment_types SET type_name = ? WHERE type_id = ?");
        $stmt->execute([$data['type_name'], $data['id']]);
        echo json_encode(['message' => 'Equipment type updated successfully']);
        break;
        
    case 'DELETE':
        // Delete equipment type
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("DELETE FROM equipment_types WHERE type_id = ?");
        $stmt->execute([$data['id']]);
        echo json_encode(['message' => 'Equipment type deleted successfully']);
        break;
        
    default:
        http_response_code(405);
        echo json_encode(['error' => 'Method not allowed']);
        break;
}
?>

