<?php
declare(strict_types=1);

/**
 * OnePanel B3: action handler group: notifications
 * Generated from legacy action.php blocks for maintainability.
 */

function op_handle_notifications(string $actionKey): void {
  switch ($actionKey) {
    case 'notification-delete':
      _op_auth_required();
      	deleteNotification($_POST['id']);
      	header("Location: dashboard.php?notifications");
      	exit;
      return;
    case 'addnotification':
      addNotification($_POST['title'], $_POST['text']);
      	header("Location: dashboard.php?notifications");
      	exit;
      return;
    case 'updatenotification':
      $active = false;
      	if ($_POST['active'] == "on") {
      		$active = true;
      	}
      	saveNotification($_POST['id'], $_POST['title'], $_POST['text'], $active);
      	header("Location: dashboard.php?notifications");
      	exit;
      return;
    default:
      http_response_code(400);
      echo 'Unknown action';
      return;
  }
}

// ---- B3.2 normalized wrappers ----

function handle_notifications_add(array $post): void {
  _op_auth_required();
  $title = op_post_string($post, 'title', 1, 200);
  $message = op_post_string($post, 'message', 1, 5000);
  op_try(function() use ($title, $message) {
    addNotification($title, $message);
  }, 'dashboard.php?notifications', 'Notification added.');
}

function handle_notifications_delete(array $post): void {
  _op_auth_required();
  $id = op_post_int($post, 'id', 1);
  op_try(function() use ($id) {
    deleteNotification($id);
  }, 'dashboard.php?notifications', 'Notification deleted.');
}

function handle_notifications_update(array $post): void {
  _op_auth_required();
  $id = op_post_int($post, 'id', 1);
  $title = op_post_string($post, 'title', 1, 200);
  $message = op_post_string($post, 'message', 1, 5000);
  op_try(function() use ($id, $title, $message) {
    saveNotification($id, $title, $message);
  }, 'dashboard.php?notifications', 'Notification updated.');
}
