<?php
require_once __DIR__ . '/../../config.php';
require_once __DIR__ . '/auth.php';
require_once FUNCTIONS_ROOT . '/functions_common.php';
function_admin_login_erfordern();

/* Verzeichnisse (storage/audio) */
$wavDirFs  = function_storage_audio_dir();

/* WAV-Dateien einsammeln */
$files = [];
if (is_dir($wavDirFs)) {
    foreach (scandir($wavDirFs) as $f) {
        if (strtolower(pathinfo($f, PATHINFO_EXTENSION)) === 'mp3') {
            $files[] = [
                'name' => $f,
                'url'  => function_storage_audio_url($f),
                'size' => filesize($wavDirFs . $f),
                'time' => filemtime($wavDirFs . $f),
            ];
        }
    }
}

/* Neueste zuerst */
usort($files, fn($a, $b) => $b['time'] <=> $a['time']);

/* TTS-Dateien aus storage/tts/ */
$ttsDirFs = function_storage_tts_dir();
$ttsFiles = [];
if (is_dir($ttsDirFs)) {
    $ttsExts = ['wav', 'mp3', 'ogg', 'm4a'];
    foreach (scandir($ttsDirFs) as $f) {
        if ($f === '.' || $f === '..' || $f === 'tts_models.json') continue;
        $ext = strtolower(pathinfo($f, PATHINFO_EXTENSION));
        if (in_array($ext, $ttsExts, true)) {
            $path = $ttsDirFs . $f;
            if (is_file($path)) {
                $ttsFiles[] = [
                    'name' => $f,
                    'url'  => function_storage_tts_url($f),
                    'size' => filesize($path),
                    'time' => filemtime($path),
                ];
            }
        }
    }
    usort($ttsFiles, fn($a, $b) => $b['time'] <=> $a['time']);
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Audio Explorer (Debug)</title>

<style>
:root{
    --bg:#0f1115;
    --panel:#151922;
    --border:#2a2f3a;
    --text:#e6e6e6;
    --accent:#4da3ff;
}

*{box-sizing:border-box}

body{
    margin:0;
    background:var(--bg);
    color:var(--text);
    font-family:system-ui,sans-serif;
    padding:24px;
}

.tabs{
    display:flex;
    gap:4px;
    margin-bottom:16px;
    border-bottom:1px solid var(--border);
}
.tabs a{
    padding:10px 20px;
    color:var(--text);
    text-decoration:none;
    border:1px solid transparent;
    border-bottom:none;
    margin-bottom:-1px;
    border-radius:6px 6px 0 0;
    background:transparent;
}
.tabs a:hover{
    background:#1a1f2c;
    color:var(--accent);
}
.tabs a.active{
    background:var(--panel);
    border-color:var(--border);
    border-bottom:1px solid var(--panel);
    color:var(--accent);
}

.box{
    background:var(--panel);
    border:1px solid var(--border);
    padding:20px;
    max-width:1000px;
}

.tab-panel{ display:none; }
.tab-panel.active{ display:block; }

h2{
    margin-top:0;
    color:var(--accent);
}

table{
    width:100%;
    border-collapse:collapse;
    margin-top:12px;
}

th,td{
    border:1px solid var(--border);
    padding:8px;
    text-align:left;
}

th{
    background:#1a1f2c;
}

audio{
    width:220px;
}

button{
    background:#1c2130;
    border:1px solid var(--border);
    color:var(--text);
    padding:6px 12px;
    cursor:pointer;
}

button:hover{
    border-color:var(--accent);
}

.small{
    opacity:.7;
    font-size:12px;
}
</style>
</head>

<body>

<nav class="tabs" role="tablist">
    <a href="#audio" class="tab-link active" data-tab="audio" role="tab">Audio (MP3)</a>
    <a href="#tts" class="tab-link" data-tab="tts" role="tab">TTS Explorer</a>
</nav>

<div id="tab-audio" class="box tab-panel active">
    <h2>MP3 Audio Player</h2>

    <?php if (empty($files)): ?>
        <p class="small">Keine mp3-Dateien gefunden.</p>
    <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th>Datei</th>
                    <th>Abspielen</th>
                    <th>Download</th>
                    <th>Größe</th>
                    <th>Erstellt</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($files as $f): ?>
                <tr>
                    <td><?= htmlspecialchars($f['name']) ?></td>
                    <td>
                        <audio controls preload="none">
                            <source src="<?= htmlspecialchars($f['url']) ?>" type="audio/mpeg">
                        </audio>
                    </td>
                    <td>
                        <a href="<?= htmlspecialchars($f['url']) ?>"
                           download="<?= htmlspecialchars($f['name']) ?>">
                            <button type="button">Download</button>
                        </a>
                    </td>
                    <td class="small">
                        <?= number_format($f['size'] / 1024, 1) ?> KB
                    </td>
                    <td class="small">
                        <?= date('Y-m-d H:i:s', $f['time']) ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<div id="tab-tts" class="box tab-panel">
    <h2>TTS Explorer</h2>
    <p class="small">Generierte TTS-Dateien aus storage/tts/ – abspielen, herunterladen.</p>
    <?php if (empty($ttsFiles)): ?>
        <p class="small">Keine TTS-Dateien gefunden.</p>
    <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th>Datei</th>
                    <th>Abspielen</th>
                    <th>Download</th>
                    <th>Größe</th>
                    <th>Erstellt</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($ttsFiles as $f): ?>
                <?php
                $ext = strtolower(pathinfo($f['name'], PATHINFO_EXTENSION));
                $mime = ['wav' => 'audio/wav', 'mp3' => 'audio/mpeg', 'ogg' => 'audio/ogg', 'm4a' => 'audio/mp4'][$ext] ?? 'audio/wav';
                ?>
                <tr>
                    <td><?= htmlspecialchars($f['name']) ?></td>
                    <td>
                        <audio controls preload="none">
                            <source src="<?= htmlspecialchars($f['url']) ?>" type="<?= htmlspecialchars($mime) ?>">
                        </audio>
                    </td>
                    <td>
                        <a href="<?= htmlspecialchars($f['url']) ?>"
                           download="<?= htmlspecialchars($f['name']) ?>">
                            <button type="button">Download</button>
                        </a>
                    </td>
                    <td class="small">
                        <?= number_format($f['size'] / 1024, 1) ?> KB
                    </td>
                    <td class="small">
                        <?= date('Y-m-d H:i:s', $f['time']) ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<script>
(function() {
    var links = document.querySelectorAll('.tab-link');
    var panels = document.querySelectorAll('.tab-panel');
    function showTab(id) {
        var hash = id || (window.location.hash && window.location.hash.slice(1)) || 'audio';
        if (hash !== 'audio' && hash !== 'tts') hash = 'audio';
        links.forEach(function(a) {
            a.classList.toggle('active', a.getAttribute('data-tab') === hash);
        });
        panels.forEach(function(p) {
            var pid = p.id.replace('tab-', '');
            p.classList.toggle('active', pid === hash);
        });
        if (window.location.hash !== '#' + hash) window.location.hash = hash;
    }
    links.forEach(function(a) {
        a.addEventListener('click', function(e) {
            e.preventDefault();
            showTab(a.getAttribute('data-tab'));
        });
    });
    window.addEventListener('hashchange', function() { showTab(); });
    showTab();
})();
</script>

</body>
</html>
