<?php
declare(strict_types=1);

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

function op_handle_vpn(string $actionKey): void {
  switch ($actionKey) {
    case 'vpn-delete':
      deleteVpn($_POST['id']);
      	header("Location: dashboard.php?vpn");
      	exit;
      return;
    case 'addvpn':
      $path = $_POST['ovpnpath'];
      	if (isset($_FILES['ovpnfile']['name']) && $_FILES['ovpnfile']['name'] != "") {
      		//first checkthe extension
      		$allowed = array('ovpn');
      		$filename = $_FILES['ovpnfile']['name'];
      		$ext = pathinfo($filename, PATHINFO_EXTENSION);
      		if (!in_array(strtolower($ext), $allowed)) {
      			echo 'Only OVPN files allowed, filename not allowed: ' . $_FILES['ovpnfile']['name'];
      			exit;
      		} else {
      			$path = time() . "." . $ext;
      			move_uploaded_file($_FILES['ovpnfile']['tmp_name'], "ovpn-files/" . $path);
      		}
      	}
      	$authEmbedded = false;
      	if ($_POST['authembedded'] == "on") {
      		$authEmbedded = true;
      	}
      	addVpn($_POST['location'],$_POST['country'], $path, $_POST['authtype'], $_POST['username'], $_POST['password'], $authEmbedded);
      	header("Location: dashboard.php?vpn");
      	exit;
      return;
    case 'updatevpn':
      $path = $_POST['ovpnpath'];
      	if (isset($_FILES['ovpnfile']['name']) && $_FILES['ovpnfile']['name'] != "") {
      		//first checkthe extension
      		$allowed = array('ovpn');
      		$filename = $_FILES['ovpnfile']['name'];
      		$ext = pathinfo($filename, PATHINFO_EXTENSION);
      		if (!in_array(strtolower($ext), $allowed)) {
      			echo 'Only OVPN files allowed, filename not allowed: ' . $_FILES['ovpnfile']['name'];
      			exit;
      		} else {
      			$path = time() . "." . $ext;
      			move_uploaded_file($_FILES['ovpnfile']['tmp_name'], "ovpn-files/" . $path);
      		}
      	}
      	$active = false;
      	if ($_POST['active'] == "on") {
      		$active = true;
      	}
      	$authEmbedded = false;
      	if ($_POST['authembedded'] == "on") {
      		$authEmbedded = true;
      	}
      	saveVpn($_POST['id'], $_POST['location'],$_POST['country'], $path, $active, $_POST['authtype'], $_POST['username'], $_POST['password'], $authEmbedded);
      	header("Location: dashboard.php?vpn");
      	exit;
      return;
    default:
      http_response_code(400);
      echo 'Unknown action';
      return;
  }
}

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

function handle_vpn_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) {
    addVPN($name, $portal, $active);
  }, 'dashboard.php?vpn', 'VPN entry added.');
}

function handle_vpn_delete(array $post): void {
  _op_auth_required();
  $id = op_post_int($post, 'id', 1);
  op_try(function() use ($id) {
    deleteVPN($id);
  }, 'dashboard.php?vpn', 'VPN entry deleted.');
}

function handle_vpn_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) {
    saveVPN($id, $name, $portal, $active);
  }, 'dashboard.php?vpn', 'VPN entry updated.');
}
