<?php
require_once '../../config/db.php';
require_once '../../includes/auth_middleware.php';

header('Content-Type: application/json');

try {
    $user_id = verifyToken();
    if (!$user_id) {
        http_response_code(401);
        echo json_encode(['error' => 'Unauthorized']);
        exit;
    }

    $sql = "SELECT r.*, u2.username as reported 
            FROM reports r 
            LEFT JOIN users u2 ON r.reported_id = u2.id
            WHERE r.reporter_id = :user_id
            ORDER BY r.created_at DESC";

    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':user_id', $user_id);
    $stmt->execute();
    
    $reports = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        'count' => count($reports),
        'reports' => $reports
    ]);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
?>
