<?php
session_start();
require_once 'config.php';
require_once 'functions.php';

$targetId   = (int)($_POST['target_id']   ?? 0);
$targetType = in_array($_POST['target_type'] ?? '', ['bet','chat','user']) ? $_POST['target_type'] : 'bet';
$reason     = in_array($_POST['reason'] ?? '', ['violent','sexual','inadequate','other']) ? $_POST['reason'] : 'other';
$notes      = trim(htmlspecialchars($_POST['notes'] ?? ''));
$reporterId = isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0 ? (int)$_SESSION['user_id'] : null;

if ($targetId) {
    $pdo->prepare("INSERT INTO reports (reporter_id, target_id, target_type, reason, notes, status) VALUES (?,?,?,?,?,'new')")
        ->execute([$reporterId, $targetId, $targetType, $reason, $notes]);

    // Si visiteur ou membre — notifier Oracle + Admin
    $admins = $pdo->query("SELECT id FROM users WHERE admin=1")->fetchAll();
    foreach ($admins as $a) {
        addNotification($pdo, (int)$a['id'], " Nouveau report ({$targetType} #{$targetId}) : {$reason}", "admin_reports.php");
    }
    // Si membre du cercle, masquer le gage
    if ($targetType === 'bet' && $reporterId) {
        // Compter les reports sur ce gage
        $cnt = $pdo->prepare("SELECT COUNT(*) FROM reports WHERE target_id=? AND target_type='bet'");
        $cnt->execute([$targetId]);
        if ((int)$cnt->fetchColumn() >= 2) {
            $pdo->prepare("UPDATE bets SET hidden=1 WHERE id=?")->execute([$targetId]);
        }
    }
}

header("Location: index.php?reported=1");
exit;
?>
