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

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

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

