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

$friendsFile = __DIR__ . '/data/friends.txt';
$imageDir = 'assets/friends/';
$friends = [];

/* Load friends data */
if (file_exists($friendsFile)) {
    $lines = file($friendsFile, FILE_IGNORE_NEW_LINES);

    foreach ($lines as $line) {
        list($id, $name, $desc, $link, $image) = explode('|', $line);

        $imgSrc = '';
        if ($image) {
            $imgSrc = str_starts_with($image, 'http')
                ? $image
                : ($imageDir . $image);
        }

        $friends[] = [
            'name' => $name,
            'desc' => $desc,
            'link' => $link,
            'image' => $imgSrc
        ];
    }
}
?>

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

        <!-- PAGE HEADER -->
        <div class="friends-banner">
            <h1 class="friends-banner-text">🤝 Friends of Oldies & More!</h1>
            <p class="friends-subtext">Stations, creators, and partners who share our passion for great music.</p>
        </div>

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

        <!-- FRIENDS GRID -->
        <?php if (!empty($friends)): ?>

            <div class="friends-grid-2">

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

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

                        <!-- NAME -->
                        <h2 class="friend-name"><?= htmlspecialchars($friend['name']) ?></h2>

                        <!-- DESCRIPTION -->
                        <p class="friend-desc"><?= nl2br(htmlspecialchars($friend['desc'])) ?></p>

                        <!-- LINK -->
                        <?php if ($friend['link']): ?>
                            <a href="<?= htmlspecialchars($friend['link']) ?>"
                               target="_blank"
                               class="button radio-static friend-btn">
                                🔗 Visit
                            </a>
                        <?php endif; ?>

                    </div>
                <?php endforeach; ?>

            </div>

        <?php else: ?>

            <div class="content-box center-text">
                <p>No friends listed yet. The airwaves are quiet… for now.</p>
            </div>

        <?php endif; ?>

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

        <!-- 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>

