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

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

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

