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

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

switch($method) {
    case 'GET':
        if(isset($_GET['id'])) {
            // Get single equipment with related info
            $stmt = $pdo->prepare("
                SELECT e.*, o.object_name, et.type_name, c.name as client_name
                FROM equipment e 
                JOIN objects o ON e.object_id = o.object_id
                JOIN equipment_types et ON e.type_id = et.type_id
                JOIN clients c ON o.client_id = c.client_id
                WHERE e.equipment_id = ?
            ");
            $stmt->execute([$_GET['id']]);
            $equipment = $stmt->fetch();
            echo json_encode($equipment);
        } else {
            // Get all equipment with related info
            $stmt = $pdo->query("
                SELECT e.*, o.object_name, et.type_name, c.name as client_name
                FROM equipment e 
                JOIN objects o ON e.object_id = o.object_id
                JOIN equipment_types et ON e.type_id = et.type_id
                JOIN clients c ON o.client_id = c.client_id
                ORDER BY e.serial_number
            ");
            $equipment = $stmt->fetchAll();
            echo json_encode($equipment);
        }
        break;
        
    case 'POST':
        // Create new equipment
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("INSERT INTO equipment (object_id, type_id, serial_number, description, installation_date) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([
            $data['object_id'],
            $data['type_id'],
            $data['serial_number'],
            $data['description'] ?? null,
            $data['installation_date'] ?? null
        ]);
        echo json_encode(['id' => $pdo->lastInsertId(), 'message' => 'Equipment created successfully']);
        break;
        
    case 'PUT':
        // Update equipment
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("UPDATE equipment SET object_id = ?, type_id = ?, serial_number = ?, description = ?, installation_date = ? WHERE equipment_id = ?");
        $stmt->execute([
            $data['object_id'],
            $data['type_id'],
            $data['serial_number'],
            $data['description'] ?? null,
            $data['installation_date'] ?? null,
            $data['id']
        ]);
        echo json_encode(['message' => 'Equipment updated successfully']);
        break;
        
    case 'DELETE':
        // Delete equipment
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("DELETE FROM equipment WHERE equipment_id = ?");
        $stmt->execute([$data['id']]);
        echo json_encode(['message' => 'Equipment deleted successfully']);
        break;
        
    default:
        http_response_code(405);
        echo json_encode(['error' => 'Method not allowed']);
        break;
}
?>

