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

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

switch($method) {
    case 'GET':
        if(isset($_GET['id'])) {
            // Get single visit with related info
            $stmt = $pdo->prepare("
                SELECT v.*, i.description as incident_description, e.serial_number, 
                       et.type_name, o.object_name, c.name as client_name
                FROM visits v 
                JOIN incidents i ON v.incident_id = i.incident_id
                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 v.visit_id = ?
            ");
            $stmt->execute([$_GET['id']]);
            $visit = $stmt->fetch();
            echo json_encode($visit);
        } else {
            // Get all visits with related info
            $stmt = $pdo->query("
                SELECT v.*, i.description as incident_description, e.serial_number, 
                       et.type_name, o.object_name, c.name as client_name
                FROM visits v 
                JOIN incidents i ON v.incident_id = i.incident_id
                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 v.visit_date DESC
            ");
            $visits = $stmt->fetchAll();
            echo json_encode($visits);
        }
        break;
        
    case 'POST':
        // Create new visit
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("INSERT INTO visits (incident_id, visit_date, technician_name, notes) VALUES (?, ?, ?, ?)");
        $stmt->execute([
            $data['incident_id'],
            $data['visit_date'],
            $data['technician_name'],
            $data['notes'] ?? null
        ]);
        echo json_encode(['id' => $pdo->lastInsertId(), 'message' => 'Visit created successfully']);
        break;
        
    case 'PUT':
        // Update visit
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("UPDATE visits SET incident_id = ?, visit_date = ?, technician_name = ?, notes = ? WHERE visit_id = ?");
        $stmt->execute([
            $data['incident_id'],
            $data['visit_date'],
            $data['technician_name'],
            $data['notes'] ?? null,
            $data['id']
        ]);
        echo json_encode(['message' => 'Visit updated successfully']);
        break;
        
    case 'DELETE':
        // Delete visit
        $data = json_decode(file_get_contents('php://input'), true);
        $stmt = $pdo->prepare("DELETE FROM visits WHERE visit_id = ?");
        $stmt->execute([$data['id']]);
        echo json_encode(['message' => 'Visit deleted successfully']);
        break;
        
    default:
        http_response_code(405);
        echo json_encode(['error' => 'Method not allowed']);
        break;
}
?>

