<?php
include 'config.php';

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

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode([]);
    exit;
}

$type = isset($_POST['type']) ? trim($_POST['type']) : '';

// Admin table থেকে owner username নেওয়া
$owner_res = $conn->query("SELECT username FROM admin LIMIT 1");
$owner_username = ($owner_res && $owner_res->num_rows > 0) ? $owner_res->fetch_assoc()['username'] : '';

if ($type === 'owner') {

    // Owner যত refund করেছে সব
    $stmt = $conn->prepare("SELECT * FROM refund_logs WHERE moderator_username = ? ORDER BY refunded_at DESC");
    $stmt->bind_param("s", $owner_username);
    $stmt->execute();
    $logs = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    $stmt->close();

    echo json_encode(count($logs) > 0 ? $logs : []);

} elseif ($type === 'moderator') {

    // Owner বাদে বাকি সব (moderator দের সব history)
    $stmt = $conn->prepare("SELECT * FROM refund_logs WHERE moderator_username != ? ORDER BY refunded_at DESC");
    $stmt->bind_param("s", $owner_username);
    $stmt->execute();
    $logs = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    $stmt->close();

    echo json_encode(count($logs) > 0 ? $logs : []);

} else {
    echo json_encode([]);
}
