<?php
require_once 'includes/functions.php';

$code = strtoupper(trim($_GET['code'] ?? ''));
$share = $code ? getShareByCode($code) : null;
$file = $share ? getFileById($share['file_id']) : null;

if ($share) {
    incrementShareAccess($code);
    logShareAccess($code);
}

if (!$share || !$file || isExpired($file)) {
    $error = true;
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DataStore — Paylaşılan Dosya</title>
<style>
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  :root {
    --bg: #0d0d0d; --surface: #141414; --surface2: #1c1c1c; --border: #2a2a2a;
    --accent: #2563eb; --text: #f0f0f0; --text-muted: #888; --danger: #ef4444; --warn: #f59e0b; --success: #22c55e;
  }
  body { font-family: 'Inter', sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
  .card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 36px; max-width: 480px; width: 100%; }
  .back-link { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-muted); text-decoration: none; margin-bottom: 24px; transition: color 0.2s; }
  .back-link:hover { color: var(--text); }
  .file-icon { font-size: 48px; margin-bottom: 14px; }
  .file-name { font-size: 20px; font-weight: 700; margin-bottom: 8px; word-break: break-all; }
  .file-meta { font-size: 13px; color: var(--text-muted); margin-bottom: 24px; line-height: 1.7; }
  .code-badge { display: inline-block; background: var(--surface2); border: 1px solid var(--border); border-radius: 6px; padding: 4px 12px; font-family: monospace; font-size: 16px; color: var(--accent); letter-spacing: 3px; font-weight: 700; margin-bottom: 10px; }
  .preview-area { background: #000; border-radius: 10px; overflow: hidden; margin-bottom: 20px; min-height: 80px; display: flex; align-items: center; justify-content: center; }
  .preview-area img, .preview-area video { max-width: 100%; max-height: 360px; object-fit: contain; }
  .preview-area audio { width: 100%; padding: 20px; }
  .preview-area iframe { width: 100%; height: 360px; border: none; }
  .no-preview { text-align: center; padding: 30px; }
  .no-preview .np-icon { font-size: 40px; margin-bottom: 10px; }
  .no-preview .np-msg { font-size: 13px; color: var(--text-muted); }
  .btn { display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 11px 20px; border-radius: 8px; font-size: 14px; font-weight: 600; font-family: inherit; cursor: pointer; border: none; text-decoration: none; transition: all 0.15s; }
  .btn-primary { background: var(--accent); color: #fff; width: 100%; }
  .btn-primary:hover { background: #1d4ed8; }
  .error-icon { font-size: 56px; margin-bottom: 16px; }
  .error-title { font-size: 22px; font-weight: 700; margin-bottom: 8px; color: var(--danger); }
  .error-sub { font-size: 14px; color: var(--text-muted); line-height: 1.6; }
  .expire-warn { font-size: 12px; color: var(--warn); margin-bottom: 16px; }
</style>
</head>
<body>
<div class="card">
  <a href="index.php" class="back-link">
    ← Ana Sayfaya Dön
  </a>

  <?php if (isset($error)): ?>
    <div style="text-align:center">
      <div class="error-icon">⚠️</div>
      <div class="error-title">Dosya Bulunamadı</div>
      <div class="error-sub">Bu paylaşım kodu geçersiz, süresi dolmuş veya dosya silinmiş olabilir.</div>
    </div>

  <?php else: ?>
    <div class="code-badge"><?= htmlspecialchars($code) ?></div>
    <div class="file-icon"><?= getMimeIcon($file['mime'], $file['ext']) ?></div>
    <div class="file-name"><?= htmlspecialchars($file['original_name']) ?></div>
    <div class="file-meta">
      Boyut: <?= formatBytes((int)$file['size']) ?><br>
      Tür: <?= htmlspecialchars($file['mime']) ?><br>
      Yükleme: <?= htmlspecialchars($file['uploaded_at']) ?>
    </div>
    <div class="expire-warn">⏳ Bu dosya <?= formatExpire($file) ?> sonra silinecek</div>

    <?php if (isPreviewable($file['mime'])): ?>
    <div class="preview-area" id="preview-area">
      <?php if (str_starts_with($file['mime'], 'image/')): ?>
        <img src="download.php?id=<?= $file['id'] ?>&code=<?= urlencode($code) ?>&preview=1" alt="Önizleme">
      <?php elseif (str_starts_with($file['mime'], 'video/')): ?>
        <video controls>
          <source src="download.php?id=<?= $file['id'] ?>&code=<?= urlencode($code) ?>&preview=1">
        </video>
      <?php elseif (str_starts_with($file['mime'], 'audio/')): ?>
        <audio controls>
          <source src="download.php?id=<?= $file['id'] ?>&code=<?= urlencode($code) ?>&preview=1">
        </audio>
      <?php elseif ($file['mime'] === 'application/pdf'): ?>
        <iframe src="download.php?id=<?= $file['id'] ?>&code=<?= urlencode($code) ?>&preview=1"></iframe>
      <?php endif; ?>
    </div>
    <?php else: ?>
    <div class="preview-area">
      <div class="no-preview">
        <div class="np-icon"><?= getMimeIcon($file['mime'], $file['ext']) ?></div>
        <div class="np-msg">Bu dosya biçimi önizlenemiyor.<br>İndirerek açabilirsiniz.</div>
      </div>
    </div>
    <?php endif; ?>

    <a href="download.php?id=<?= $file['id'] ?>&code=<?= urlencode($code) ?>" class="btn btn-primary">
      ⬇ Dosyayı İndir
    </a>
  <?php endif; ?>
</div>
</body>
</html>
