<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

$djsFile = __DIR__ . '/data/djs.txt';

$djs = [];

/* Load DJ data: 
   Format per entry:
   Name
   Bio (one or more lines)
   Image path or URL
   ---
*/
if (file_exists($djsFile)) {
    $raw = file_get_contents($djsFile);

    // Split entries on lines that are exactly "---"
    $entries = preg_split('/^\s*---\s*$/m', $raw);

    foreach ($entries as $entry) {
        $entry = trim($entry);
        if ($entry === '') {
            continue;
        }

        $lines = array_filter(explode("\n", $entry), 'strlen');
        $lines = array_map('trim', $lines);

        if (count($lines) < 2) {
            continue;
        }

        // First line = name
        $name = array_shift($lines);

        // Last line = image (if looks like path or URL)
        $image = array_pop($lines);

        // Remaining lines = bio
        $bio = trim(implode("\n", $lines));

        // Resolve image as-is (we’re storing full or relative web path in djs.txt)
        $imgSrc = '';
        if (!empty($image)) {
            if (filter_var($image, FILTER_VALIDATE_URL)) {
                $imgSrc = $image;
            } else {
                // Treat as web path, e.g. assets/djs/filename.jpg
                $imgSrc = $image;
            }
        }

        $djs[] = [
            'name'  => $name,
            'bio'   => $bio,
            'image' => $imgSrc
        ];
    }
}
?>

<div class="main-wrap">
    <div class="content-panel">

        <!-- PAGE HEADER -->
        <div class="dj-banner">
            <h1 class="dj-banner-text">🎧 Meet the DJs</h1>
            <p class="dj-subtext">The voices behind Oldies and More!</p>
        </div>

        <div class="vinyl-divider center-divider"></div>

        <!-- DJ LIST -->
        <?php if (!empty($djs)): ?>

            <?php foreach ($djs as $dj): ?>
                <div class="neon-card dj-card">

                    <!-- DJ IMAGE -->
                    <?php if (!empty($dj['image'])): ?>
                        <div class="dj-image-wrap">
                            <img src="<?= htmlspecialchars($dj['image']) ?>"
                                 alt="<?= htmlspecialchars($dj['name']) ?>"
                                 class="dj-image">
                        </div>
                    <?php endif; ?>

                    <!-- DJ NAME -->
                    <h2 class="section-title"><?= htmlspecialchars($dj['name']) ?></h2>

                    <!-- BIO LABEL -->
                    <p><strong style="color:#4ff;">Bio:</strong></p>

                    <!-- DJ BIO -->
                    <p class="about-text"><?= nl2br(htmlspecialchars($dj['bio'])) ?></p>

                </div>

                <div class="vinyl-divider center-divider"></div>
            <?php endforeach; ?>

        <?php else: ?>

            <div class="content-box center-text">
                <p>No DJs listed yet. Stay tuned…</p>
            </div>

        <?php endif; ?>

        <!-- BACK BUTTON -->
        <div class="content-box center-text">
            <a href="index.php" class="button radio-static">⬅ Back to Home</a>
        </div>

        <?php include __DIR__ . '/includes/footer.php'; ?>
    </div>
</div>
