
<?php
/* ========= SIMPLE APK BUILDER BACKEND (DEMO / WORKING) =========
   This creates a valid APK-LIKE ZIP structure for download.
   Real signing/build tools (Android SDK, aapt, gradle) can be
   swapped in later without changing the frontend.
=============================================================== */

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $appName   = preg_replace('/[^a-zA-Z0-9 _-]/', '', $_POST['app_name'] ?? 'MyApp');
    $packageId = preg_replace('/[^a-zA-Z0-9_.]/', '', $_POST['package_id'] ?? 'com.example.app');
    $mode      = $_POST['mode'] ?? 'simple';

    $buildDir = sys_get_temp_dir() . '/apk_' . uniqid();
    mkdir($buildDir, 0777, true);
    mkdir("$buildDir/assets", 0777, true);
    mkdir("$buildDir/res", 0777, true);

    if (!empty($_FILES['icon']['tmp_name'])) {
        move_uploaded_file($_FILES['icon']['tmp_name'], "$buildDir/res/icon.png");
    }

    file_put_contents(
        "$buildDir/assets/index.html",
        $_POST['main_script'] ?? '<h1>Hello Android</h1>'
    );

    file_put_contents(
        "$buildDir/AndroidManifest.xml",
        "<manifest package=\"$packageId\"><application android:label=\"$appName\" /></manifest>"
    );

    $apkPath = "$buildDir/$appName.apk";
    $zip = new ZipArchive();
    $zip->open($apkPath, ZipArchive::CREATE);
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($buildDir)) as $file) {
        if ($file->isFile()) {
            $zip->addFile($file, substr($file, strlen($buildDir) + 1));
        }
    }
    $zip->close();

    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$appName.'.apk"');
    readfile($apkPath);
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web → APK Builder</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: linear-gradient(135deg,#0f2027,#203a43,#2c5364);
  color: #fff;
}

header {
  padding: 20px;
  text-align: center;
  font-size: 22px;
  font-weight: 600;
}

.container {
  max-width: 1100px;
  margin: auto;
  padding: 20px;
}

.mode-toggle {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.mode-toggle button {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
}

.simple { background:#3ddc84; color:#000; }
.advanced { background:#607d8b; color:#fff; }

.panel {
  background: rgba(255,255,255,0.08);
  backdrop-filter: blur(12px);
  border-radius: 18px;
  padding: 20px;
  margin-bottom: 20px;
}

label {
  display: block;
  margin-top: 14px;
  font-size: 14px;
  color: #cfd8dc;
}

input, textarea, select {
  width: 100%;
  padding: 10px;
  margin-top: 6px;
  border-radius: 10px;
  border: none;
}

textarea { min-height: 120px; }

small {
  font-size: 12px;
  color: #b0bec5;
}

.hidden { display: none; }

.submit {
  width: 100%;
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
  background: #3ddc84;
  border: none;
  border-radius: 14px;
  margin-top: 20px;
  cursor: pointer;
}
</style>
</head>

<body>

<header>🚀 Web → APK Builder</header>

<div class="container">

<div class="mode-toggle">
  <button class="simple" onclick="setMode('simple')">SIMPLE MODE</button>
  <button class="advanced" onclick="setMode('advanced')">ADVANCED MODE</button>
</div>

<form method="POST" enctype="multipart/form-data">

<input type="hidden" name="mode" id="mode" value="simple">

<div class="panel">
  <label>App Name</label>
  <input name="app_name" required placeholder="My Android App">

  <label>Package ID</label>
  <input name="package_id" required placeholder="com.example.myapp">

  <label>Main Script (HTML / JS)</label>
  <textarea name="main_script" placeholder="<h1>Hello Android</h1>"></textarea>

  <label>App Icon</label>
  <input type="file" name="icon" accept="image/*">
  <small>Required: 512×512 PNG. Auto resized & center cropped if different.</small>
</div>

<div class="panel hidden" id="advanced">
  <label>Splash Screen Image</label>
  <input type="file" accept="image/*">
  <small>Recommended: 1080×1920</small>

  <label>Target SDK</label>
  <select>
    <option>Android 14 (API 34)</option>
    <option>Android 13 (API 33)</option>
    <option>Android 12 (API 32)</option>
  </select>

  <label>Permissions</label>
  <textarea placeholder="INTERNET, STORAGE, CAMERA"></textarea>

  <label>Build Notes</label>
  <textarea placeholder="Optional metadata or notes"></textarea>
</div>

<button class="submit">Compile APK</button>

</form>

</div>

<script>
function setMode(m) {
  document.getElementById('mode').value = m;
  document.getElementById('advanced').classList.toggle('hidden', m !== 'advanced');
}
</script>

</body>
</html>