<?php
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

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

// Require valid auth token
requireAuth();

$device_id = $_GET['device_id'] ?? '';

if ($device_id === '') {
    echo json_encode(['success' => false, 'message' => 'Missing device_id']);
    exit;
}

try {
    $stmt = $pdo->prepare("SELECT * FROM messegedetails WHERE device_id = ? AND type = 'sms' ORDER BY date DESC LIMIT 200");
    $stmt->execute([$device_id]);
    $messages = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode(['success' => true, 'messages' => $messages]);
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Failed to fetch messages']);
}

