<?php
declare(strict_types=1);

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

function op_handle_dns(string $actionKey): void {
  switch ($actionKey) {
    case 'dns-delete':
      _op_auth_required();
      	deleteDNS($_POST['id']);
      	header("Location: dashboard.php?dns");
      	exit;
      return;
    case 'adddns':
      addDNS($_POST['name'], $_POST['portal']);
      	header("Location: dashboard.php?dns");
      	exit;
      return;
    case 'updatedns':
      $active = false;
      	if ($_POST['active'] == "on") {
      		$active = true;
      	}
      	saveDNS($_POST['id'], $_POST['name'], $_POST['portal'], $active);
      	header("Location: dashboard.php?dns");
      	exit;
      return;
    default:
      http_response_code(400);
      echo 'Unknown action';
      return;
  }
}

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

function handle_dns_add(array $post): void {
  _op_auth_required();
  $name = op_post_string($post, 'name', 1, 200);
  $portal = op_post_url($post, 'portal', 2048);
  $active = op_post_bool_checkbox($post, 'active');
  op_try(function() use ($name, $portal, $active) {
    addDNS($name, $portal, $active);
  }, 'dashboard.php?dns', 'DNS entry added.');
}

function handle_dns_delete(array $post): void {
  _op_auth_required();
  $id = op_post_int($post, 'id', 1);
  op_try(function() use ($id) {
    deleteDNS($id);
  }, 'dashboard.php?dns', 'DNS entry deleted.');
}

function handle_dns_update(array $post): void {
  _op_auth_required();
  $id = op_post_int($post, 'id', 1);
  $name = op_post_string($post, 'name', 1, 200);
  $portal = op_post_url($post, 'portal', 2048);
  $active = op_post_bool_checkbox($post, 'active');
  op_try(function() use ($id, $name, $portal, $active) {
    saveDNS($id, $name, $portal, $active);
  }, 'dashboard.php?dns', 'DNS entry updated.');
}
