<?php
require_once '../../config/db.php';
require_once '../../core/Auth.php';

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

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['error' => 'Method not allowed']);
    exit;
}

try {
    $reporter_id = Auth::check();
    $data = json_decode(file_get_contents("php://input"));
    
    if (!isset($data->type)) {
        http_response_code(400);
        echo json_encode(['error' => 'Missing type']);
        exit;
    }

    $type = $data->type;
    $description = isset($data->description) ? trim($data->description) : '';
    $reported_id = isset($data->reported_id) ? (int)$data->reported_id : null;

    $sql = "INSERT INTO reports (reporter_id, reported_id, type, description) VALUES (?, ?, ?, ?)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$reporter_id, $reported_id, $type, $description]);
    
    echo json_encode([
        'status' => 'success',
        'report_id' => $pdo->lastInsertId(),
        'message' => 'Report submitted successfully'
    ]);

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