<?php
declare(strict_types=1);

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

function op_handle_ads(string $actionKey): void {
  switch ($actionKey) {
    case 'ad-delete':
      _op_auth_required();
      	deleteAd($_POST['id']);
      	header("Location: dashboard.php?ads");
      	exit;
      return;
    case 'addad':
      $path = "";
      	if (isset($_FILES['adimage']['name']) && $_FILES['adimage']['name'] != "") {
      		//first checkthe extension
      		$allowed = array('jpeg', 'png', 'jpg');
      		$filename = $_FILES['adimage']['name'];
      		$ext = pathinfo($filename, PATHINFO_EXTENSION);
      		if (!in_array(strtolower($ext), $allowed)) {
      			echo 'Only PNG and JPG/JPEG allowed, filename not allowed: ' . $_FILES['adimage']['name'];
      			exit;
      		} else {
      			$path = time() . "." . $ext;
      			move_uploaded_file($_FILES['adimage']['tmp_name'], "ad-images/" . $path);
      		}
      	}
      	addAd($_POST['name'], $path);
      	header("Location: dashboard.php?ads");
      	exit;
      return;
    case 'updatead':
      $path = "";
      	if (isset($_FILES['adimage']['name']) && $_FILES['adimage']['name'] != "") {
      		//first checkthe extension
      		$allowed = array('jpeg', 'png', 'jpg');
      		$filename = $_FILES['adimage']['name'];
      		$ext = pathinfo($filename, PATHINFO_EXTENSION);
      		if (!in_array(strtolower($ext), $allowed)) {
      			echo 'Only PNG and JPG/JPEG allowed, filename not allowed: ' . $_FILES['adimage']['name'];
      			exit;
      		} else {
      			$path = time() . "." . $ext;
      			move_uploaded_file($_FILES['adimage']['tmp_name'], "ad-images/" . $path);
      		}
      	}
      	$active = false;
      	if ($_POST['active'] == "on") {
      		$active = true;
      	}
      	saveAd($_POST['id'], $_POST['name'], $path, $active);
      	header("Location: dashboard.php?ads");
      	exit;
      return;
    default:
      http_response_code(400);
      echo 'Unknown action';
      return;
  }
}

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

function handle_ads_add(array $post): void {
  _op_auth_required();
  $name = op_post_string($post, 'name', 1, 200);
  $code = op_post_string($post, 'code', 1, 10000);
  $active = op_post_bool_checkbox($post, 'active');
  op_try(function() use ($name, $code, $active) {
    addAd($name, $code, $active);
  }, 'dashboard.php?ads', 'Ad added.');
}

function handle_ads_delete(array $post): void {
  _op_auth_required();
  $id = op_post_int($post, 'id', 1);
  op_try(function() use ($id) {
    deleteAd($id);
  }, 'dashboard.php?ads', 'Ad deleted.');
}

function handle_ads_update(array $post): void {
  _op_auth_required();
  $id = op_post_int($post, 'id', 1);
  $name = op_post_string($post, 'name', 1, 200);
  $code = op_post_string($post, 'code', 1, 10000);
  $active = op_post_bool_checkbox($post, 'active');
  op_try(function() use ($id, $name, $code, $active) {
    saveAd($id, $name, $code, $active);
  }, 'dashboard.php?ads', 'Ad updated.');
}
