<?php
require_once 'includes/db.php';
requireLogin();

if (isset($_POST['broadcast'])) {
    $title = $_POST['title'];
    $message = $_POST['message'];
    
    // Call API via cURL or direct logic
    // Using simple cURL to API endpoint assuming we have admin token would be best, 
    // but we can just use the OneSignalService logic here if we copy it or include it.
    // For simplicity in this monorepo structure where we can access the service file:
    require_once __DIR__ . '/../backend-php/src/Utils/Env.php';
    require_once __DIR__ . '/../backend-php/src/Services/OneSignalService.php';
    
    // We need to load Env for OneSignalService
    \App\Utils\Env::load(__DIR__ . '/../.env');
    $os = new \App\Services\OneSignalService();
    $os->broadcast($title, $message);
    
    $success = "Broadcast sent!";
}

require_once 'includes/header.php';
?>

<h2>Broadcast Notification</h2>
<?php if(isset($success)): ?>
    <div class="alert alert-success"><?= $success ?></div>
<?php endif; ?>

<form method="post">
    <div class="mb-3">
        <label class="form-label">Title</label>
        <input type="text" name="title" class="form-control" required>
    </div>
    <div class="mb-3">
        <label class="form-label">Message</label>
        <textarea name="message" class="form-control" rows="4" required></textarea>
    </div>
    <button type="submit" name="broadcast" class="btn btn-warning">Send Broadcast</button>
</form>

<?php require_once 'includes/footer.php'; ?>