<?php
declare(strict_types=1);

$tenant = null;
try {
    require_once __DIR__ . '/lib/tenant.php';
    $tenant = resolve_tenant_from_host();
} catch (Throwable $e) {
    $tenant = null;
}
if (!defined('APP_BASE')) {
    define('APP_BASE', '');
}

$host = strtolower((string) ($_SERVER['HTTP_HOST'] ?? ''));
$host = preg_replace('/:\d+$/', '', $host) ?? $host;
$hostSlug = '';
if (preg_match('/^([a-z0-9-]+)\.barber-shop\.us$/', $host, $m) && $m[1] !== 'www') {
    $hostSlug = $m[1];
}

$slug = strtolower(trim((string) ($tenant['subdomain'] ?? '')));
if ($slug === '' || $slug === 'yourshop') {
    $slug = $hostSlug;
}
$shopName = $tenant ? shop_company_name(null, $tenant) : 'this shop';
$appUrl = 'https://barber-shop.us/download-app.php';
$apkUrl = 'https://barber-shop.us/download-apk.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Get the Android app · <?= htmlspecialchars($shopName) ?></title>
  <style>
    body { margin:0; font-family:system-ui,sans-serif; background:#f6f1e8; color:#1a1410; }
    main { max-width:32rem; margin:0 auto; padding:2rem 1.25rem 3rem; line-height:1.5; }
    h1 { font-size:1.5rem; margin:0 0 .4rem; }
    .btn {
      display:block; text-align:center; background:#1a1410; color:#fff;
      text-decoration:none; padding:.95rem 1rem; border-radius:10px;
      font-weight:700; margin:1rem 0;
    }
    .card { background:#fff; border:1px solid #e4d8c4; border-radius:12px; padding:1rem 1.1rem; margin:1rem 0; }
    .muted { color:#6b5e4e; }
    code { background:#efe6d6; padding:.12rem .4rem; border-radius:4px; }
    ol { padding-left:1.2rem; }
  </style>
</head>
<body>
<main>
  <p class="muted"><a href="<?= htmlspecialchars(APP_BASE === '' ? '/' : APP_BASE . '/') ?>">← <?= htmlspecialchars($shopName) ?></a></p>
  <h1>Install the BarberShop app</h1>
  <p>This is the customer app for <strong><?= htmlspecialchars($shopName) ?></strong>. After install, enter shop name:</p>
  <p><code><?= htmlspecialchars($slug !== '' ? $slug : 'yourshop') ?></code></p>
  <a class="btn" href="<?= htmlspecialchars($apkUrl) ?>">Download Android app</a>
  <p class="muted">Official file from <a href="<?= htmlspecialchars($appUrl) ?>">barber-shop.us/download-app.php</a>. Same app for every shop on the platform.</p>
  <div class="card">
    <h2>On the phone</h2>
    <ol>
      <li>Chrome may say the file is uncommon or might be harmful. Choose <strong>Download anyway</strong> / <strong>Keep</strong>. That warning is Android/Chrome, not a virus scan of this shop.</li>
      <li>If install is blocked: Settings → allow Chrome (or Files) to install unknown apps.</li>
      <li>Play Protect may say it does not recognize the app. Choose <strong>Install anyway</strong>. This lasts until the app is on Google Play.</li>
      <li>Open BarberShop and type <code><?= htmlspecialchars($slug !== '' ? $slug : 'yourshop') ?></code>.</li>
      <li>Create an account or sign in as a <strong>customer</strong>. Shop owners use the website admin, not this app.</li>
    </ol>
  </div>
</main>
</body>
</html>
